1. 程式人生 > >序列化(boost serialization) stl

序列化(boost serialization) stl

#include <boost/serialization/map.hpp> #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 <fstream> #include <sstream> #include <map> #include <string>

typedef std::map<std::string, std::string> map_type;

int main(int argc, char** argv) {     map_type newMap;     map_type newMap_text;     map_type newMap_binary;

    newMap["1"] = "123";     newMap["2"] = "234";     newMap["3"] = "345";

    {         std::ofstream ofs("data");         boost::archive::text_oarchive oa(ofs);         oa << newMap;     }

    {         std::ifstream ifs("data");         boost::archive::text_iarchive ia(ifs);         ia >> newMap_text;     }

    {         int a = 0xfffeff;         std::ofstream ofs("data_bin");         boost::archive::binary_oarchive oa(ofs);         oa << newMap;     }

    {         std::ifstream ifs("data_bin");         boost::archive::binary_iarchive ia(ifs);         ia >> newMap_binary;     }     return 0; }

編譯:

g++ t3.cpp -lboost_serialization  -std=c++11