如何将STL映射保存到文件

Jia*_*ong 2 stl file map

是否可以将STL映射保存到文件?我可以加载文件来映射以节省时间.谢谢!

Die*_*lla 5

好吧,你可以手动完成.如果没有,最简单的方法是使用boost.serialization,它支持所有标准容器:

std::ofstream ofs("output_file");

// create class instance
std::map<int,string> whatever;

// populate map.

// save data to archive
{
    boost::archive::text_oarchive oa(ofs);
    // write map instance to archive
    oa << whatever;
    // archive and stream closed when destructors are called
}
Run Code Online (Sandbox Code Playgroud)

您可以看到反向如何工作(从存档读取),以及您还可以使用二进制存档.(您还需要一堆包含,但您可以从文档中获取这些内容.)