【发布时间】:2016-11-01 00:47:03
【问题描述】:
我正在尝试使用 boost 库来序列化 std::map 以便可以将其存储在文件中。但是我的行为很奇怪(我猜)。所以这是我的代码:
#include <map>
#include <fstream>
#include <iostream>
#include <bitset>
#include <boost/serialization/map.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
std::map<int,int> map = {{65,2}, {69,1}, {75,1} ,{77,1}, {82,1}, {84,2}, {89,2}};
void saveMapToFile(std::ofstream& f);
int main()
{
std::ofstream f("test.txt", std::ios::binary);
saveMapToFile(f);
std::cout << "position: " << f.tellp() << std::endl;
}
void saveMapToFile(std::ofstream& f)
{
std::cout << "position : " << f.tellp() << std::endl;
boost::archive::text_oarchive oarch(f);
std::cout << "position : " << f.tellp() << std::endl;
oarch << map;
std::cout << "position : " << f.tellp() << std::endl;
}
这是上面代码的输出:
position : 0
position : 28
position : 75
position: 76
那么有人可以向我解释这里发生了什么吗?为什么在将地图(在函数中)插入到不同的外部之后定位?我没有做任何额外的操作,但是那个指针又多了一个字节......我错过了什么吗?提前感谢您的帮助。
【问题讨论】:
标签: c++ serialization boost