Skip to content

Instantly share code, notes, and snippets.

@chiehmin
Created August 14, 2017 08:02
Show Gist options
  • Save chiehmin/b706573b3db1cc2edd4b17b47842a0df to your computer and use it in GitHub Desktop.
Save chiehmin/b706573b3db1cc2edd4b17b47842a0df to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
using namespace std;
class Bus {
public:
string name;
int count;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::make_nvp("name", name);
ar & boost::serialization::make_nvp("count", count);
}
};
int main()
{
Bus b;
b.name = "fuck";
b.count = 100;
// serialize
{
ofstream ofs("bus");
boost::archive::xml_oarchive oa(ofs);
oa << boost::serialization::make_nvp("bus", b);
}
Bus b2;
// deserialize
{
ifstream ifs("bus");
boost::archive::xml_iarchive ia(ifs);
ia >> boost::serialization::make_nvp("bus", b2);
cout << b2.name << ' ' << b2.count << endl;
}
return 0;
}
// compile: g++ --std=c++14 boost_serilization.cpp -lboost_serialization
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment