如果您不怕BOOST,请尝试BOOST Serialize :(模板代码,这里可能有些错误...)
#include
#include
#include
#include
#include
struct customType{
string string1;
string string2;
int i;
list list;
// boost serialize
private:
friend class boost::serialization::access;
template void serialize(Archive &ar, const unsigned int version) {
ar & string1;
ar & string2;
ar & i;
ar & list;
}
};
template
int Save(const string fname, const ClassTo &c)
{
ofstream f(fname.c_str(), ios::binary);
if (f.fail()) return -1;
boost::archive::binary_oarchive oa(f);
oa << c;
return 0;
}
用法:
Save< map >("test.map", yourMap);