#include #include #include #include #include void test_vector() { typedef std::vector container_type; typedef container_type::value_type value_type; container_type c; c.push_back(value_type(1)); c.push_back(value_type(2)); c.push_back(value_type(3)); c.push_back(value_type(3)); for (container_type::const_iterator it=c.begin(); it!=c.end(); it++) { std::cout << *it << std::endl; } { std::ofstream ofs("vfilename"); // save data to archive boost::archive::text_oarchive oa(ofs); // write class instance to archive oa << c; // archive and stream closed when destructors are called } // ... some time later restore the class instance to its orginal state container_type c2; { // create and open an archive for input std::ifstream ifs("vfilename"); boost::archive::text_iarchive ia(ifs); // read class state from archive ia >> c2; // archive and stream closed when destructors are called } for (container_type::const_iterator it=c2.begin(); it!=c2.end(); it++) { std::cout << *it << std::endl; } } #include void test_set() { typedef std::set container_type; typedef container_type::value_type value_type; container_type c; c.insert(value_type(1)); c.insert(value_type(2)); c.insert(value_type(3)); c.insert(value_type(3)); for (container_type::const_iterator it=c.begin(); it!=c.end(); it++) { std::cout << *it << std::endl; } { std::ofstream ofs("sfilename"); // save data to archive boost::archive::text_oarchive oa(ofs); // write class instance to archive oa << c; // archive and stream closed when destructors are called } // ... some time later restore the class instance to its orginal state container_type c2; { // create and open an archive for input std::ifstream ifs("sfilename"); boost::archive::text_iarchive ia(ifs); // read class state from archive ia >> c2; // archive and stream closed when destructors are called } for (container_type::const_iterator it=c2.begin(); it!=c2.end(); it++) { std::cout << *it << std::endl; } } #include void test_map() { // typedef std::map container_type; typedef std::multimap container_type; typedef container_type::value_type value_type; container_type c; c.insert(value_type(1,10)); c.insert(value_type(2,20)); c.insert(value_type(3,50)); c.insert(value_type(3,150)); for (container_type::const_iterator it=c.begin(); it!=c.end(); it++) { std::cout << (*it).first << " => " << (*it).second << std::endl; } { std::ofstream ofs("mfilename"); // save data to archive boost::archive::text_oarchive oa(ofs); // write class instance to archive oa << c; // archive and stream closed when destructors are called } // ... some time later restore the class instance to its orginal state container_type c2; { // create and open an archive for input std::ifstream ifs("mfilename"); boost::archive::text_iarchive ia(ifs); // read class state from archive ia >> c2; // archive and stream closed when destructors are called } for (container_type::const_iterator it=c2.begin(); it!=c2.end(); it++) { std::cout << (*it).first << " => " << (*it).second << std::endl; } } int main() { test_vector(); test_set(); test_map(); return 0; }