私は次のようにクラス内で宣言されたマップを持っています:
class Example {
public:
Example()
{
std::map< std::string, std::string > map_data;
map_data["LOCATION"] = "USA";
map_data["WEBSITE"] = "http://www.google.com/";
custom_map["nickb"] = map_data;
}
std::map< std::string, std::map< std::string, std::string > > get_map() { return custom_map; }
private:
std::map< std::string, std::map< std::string, std::string > > custom_map;
friend class boost::serialization::access;
template<class Archive>
void serialize( Archive &ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP( custom_map);
}
};
そして、ブーストを使用して、それを変数にマップするだけでシリアル化できるようにしたいと思います。
例はクラス全体をシリアル化しているようですが、これを行う必要はありません。また、ファイルに書き込んでいますが、マップの状態をファイルにアーカイブする必要がないため、後で復元できるように表現するだけで、非効率に思えます。
今、私は地図を保存するためにこれを持っています:
// Create an Example object
Example obj;
// Save the map
std::stringstream outstream( std::stringstream::out | std::stringstream::binary);
boost::archive::text_oarchive oa( outstream);
oa << obj; // <-- BOOST SERIALIZATION STATIC WARNING HERE
// Map saved to this string:
std::string saved_map = outstream.str();
そしてこれを復元するには:
// Now retore the map
std::map< std::string, std::map< std::string, std::string > > restored_map;
std::stringstream instream( saved_map, std::stringstream::in | std::stringstream::binary);
boost::archive::text_iarchive ia( instream);
ia >> restored_map;
std::map< std::string, std::string > map_data = restored_map.find( "nickb")->second;
std::cout << "nickb " << map_data["LOCATION"] << " " << map_data["WEBSITE"] << std::endl;
しかし、それは機能していません。誰かが私にいくつかのヒントを教えたり、マップをシリアル化して復元する方法を教えてもらえますか?
ありがとうございました。
編集: 私は私の例をより詳細に更新し、K-balloとKarl Knechtelからの回答を考慮に入れました(ありがとう!)。これにより、上記のコメント行でのブーストシリアル化の静的警告である1つを除いて、ほとんどすべてのエラーが解決されました。警告は次のとおりです。
[Warning] comparison between signed and unsigned integer expressions
この警告を解決してコンパイルする方法はありますか?ありがとう!
編集: 私の問題は2つありました:追加する必要がありました:BOOST_CLASS_TRACKING(例、track_never)そして、クラス全体をシリアル化し、マップのシリアル化を解除しようとしていました。