-
- #include "stdafx.h"
-
- #include <boost/archive/text_oarchive.hpp>
- #include <boost/archive/text_iarchive.hpp>
- #include <boost/serialization/vector.hpp>
-
- #include <string>
- #include <vector>
- #include <iostream>
- #include <sstream>
- #include <fstream>
-
-
- struct MyRecord
- {
- std::string a;
- std::string b;
-
- private:
- friend class boost::serialization::access;
- template<class Archive>
- void serialize(Archive & ar, const unsigned int version)
- {
- ar & a;
- ar & b;
- }
- };
-
- struct MyData{
- int id;
- std::string strName;
- std::string strValue;
-
- MyData() {}
- MyData(int id, std::string strName, std::string strValue)
- {
- this->id = id;
- this->strName = strName;
- this->strValue = strValue;
- }
-
- std::vector<MyRecord> listMR;
-
- private:
- friend boost::serialization::access;
- template<typename Archive>
- void serialize(Archive &ar,const unsigned int version)
- {
- ar & id;
- ar & strName & strValue;
- ar & listMR;
- }
- };
-
- void printTest(MyData &mt)
- {
- std::cout<<"a="<<mt.id<<" uid="<<mt.strName.c_str()<<" usr="<<mt.strValue.c_str()<<std::endl;
-
- std::vector<MyRecord>::iterator iter = mt.listMR.begin();
- while(iter!=mt.listMR.end())
- {
- std::cout<<"=> "<<iter->a.c_str()<<" "<<iter->b.c_str()<<std::endl;
- iter++;
- }
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- std::stringstream ss;
-
-
- MyData p1(11,"anderson","neo");
-
- MyRecord mr,mr2;
- mr.a="apple",mr.b="第一条记录";
- mr2.a = "this is b",mr2.b = "第二条记录";
- p1.listMR.push_back(mr);
- p1.listMR.push_back(mr2);
-
- boost::archive::text_oarchive(ss) << p1;
-
-
-
-
-
-
-
-
- MyData p2;
- boost::archive::text_iarchive(ss) >> p2;
-
- printTest(p2);
- std::cout << "序列化后的=>" << ss.str() << std::endl;;
-
- return 0;
- }
http://blog.csdn.net/lee353086/article/details/38421095
测试了下boost的序列化反序列化功能
原文:http://www.cnblogs.com/findumars/p/7635864.html