如果序列化的std :: map太大,则无法反序列化std :: map

时间:2019-06-09 09:35:10

标签: c++ boost stl

我尝试反序列化先前序列化的std :: map对象。但是,当序列化std :: map时,其元素要多于ca。 10000程序崩溃。

#include <iostream>
#include <string>
#include <sstream>

#include <thread>
#include <Python.h>
#include <fstream>
#include <algorithm>
#include <boost/serialization/map.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <map>

constant std::string filename = "Dummy";
constexpr int threshold = 10000;

void SaveMapToFile()
{
    std::ifstream file(filename);
    std::string line; int i = 0;
    while (std::getline(file, line))
    {
        auto place = std::find(line.begin(), line.end(), '\t');
        std::string line2(line.begin(), place);
        std::string line3(place + 1, line.end());
        tMap[line2] = line3;
        i++;
        if (i > threshold) {
            break;
        }
    }
    std::ofstream ss("map.txt");
    boost::archive::binary_oarchive temp(ss);
    temp << tMap;
}

void LoadMapFromFile()
{
    std::cout << "Before loading" << std::endl;
    std::ifstream ss("map.txt");
    boost::archive::binary_iarchive temp(ss);
    std::map<std::string, std::string> tMap2;
    temp >> tMap2;
    std::cout << "After loading" << std::endl;
}

输出如下:

Before loading

然后程序停止。

但是,如果将阈值设置为较低的值,例如1000,一切正常:

Before loading
After loading

这种行为的原因是什么?

0 个答案:

没有答案