#include <fstream>
#include <map>
#include <vector>
using namespace std;
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
class testclass;
struct myinfo;
namespace boost
{
namespace serialization
{
template<typename Archive>
void serialize(Archive & ar, testclass & g, const unsigned int version)
{
ar & g.m_mymap;
}
template<typename Archive>
void serialize(Archive & ar, myinfo & g, const unsigned int version)
{
ar & g.age;
ar & g.name;
}
}
}
typedef struct myinfo
{
int age;
string name;
}myinfo;
class testclass
{
public:
void setData()
{
myinfo info = {100, "chenhuizong"};
vector<myinfo> vec;
vec.push_back(info);
vec.push_back(info);
m_mymap["1"] = vec;
m_mymap["2"] = vec;
m_mymap["3"] = vec;
}
testclass(){};
map<string, vector<myinfo> > m_mymap;
};
int main() {
std::ofstream ofs("./sel.dat", std::ios::binary);
testclass g;
g.setData();
{
boost::archive::binary_oarchive oa(ofs);
oa << g;
}
testclass newg;
{
std::ifstream ifs("./sel.dat", std::ios::binary);
boost::archive::binary_iarchive ia(ifs);
ia >> newg;
}
return 0;
}
boost系列化c++对象
最新推荐文章于 2021-08-27 18:20:10 发布