Boost::serialization std::unordered_map<double, std::vector<double>>
Boost::serialization of an std::unordered_map<double, std::vector<double>>
我正在尝试使用 boost::serialize 序列化一个简单的 std::unordered_map>。我设法保存地图就好了。当我尝试加载它时出现问题。
这是我编写的代码:
double E = 250E-4;
std::vector<double> init_vals(2,0.0);
std::unordered_map<double, std::vector<double>> map;
std::ofstream ofs("energy_map");
boost::archive::binary_oarchive oa(ofs);
map.emplace(E, init_vals);
oa << map;
我加载地图的代码是这样的:
std::ifstream ifs("energy_map");
std::unordered_map <double, std::vector<double>> map;
boost::archive::binary_iarchive ia(ifs);
ia >> map;
我得到 boost::archive::archive_exception 行 "ia >> map;"。异常是 "unregistered_cast" 异常,根据 boost 文档指出:
// base - derived relationship not registered with
// void_cast_register
I'm not quite sure why this isn't working out cause it seems fairly simple. (I've edited the code from my original files to make it simpler, but the lines are identical to that within my code).
感谢任何帮助。谢谢!
您很可能/还/序列化了其他一些多态类型。 SSCCE 示例显示这适用于 GCC:
http://paste.ubuntu.com/12963866/
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/unordered_map.hpp>
#include <boost/serialization/vector.hpp>
#include <fstream>
#include <iostream>
void write_map() {
std::unordered_map<double, std::vector<double> > map;
{
double E = 250E-4;
std::vector<double> init_vals(2, 0.0);
map.emplace(E, init_vals);
}
{
std::ofstream ofs("energy_map");
boost::archive::binary_oarchive oa(ofs);
oa << map;
}
}
void load_map() {
std::unordered_map<double, std::vector<double> > map;
{
std::ifstream ifs("energy_map");
boost::archive::binary_iarchive ia(ifs);
ia >> map;
}
for (auto &p : map) {
std::cout << p.first << " -> { ";
std::copy(p.second.begin(), p.second.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << "}\n";
}
}
int main() {
write_map();
load_map();
}
因此,如果您仍有问题,请查看
- 对于Undefined Behaviour
- 对于 ABI 问题(您是否使用与主程序相同的 compiler/library 版本和标志编译 boost)
我正在尝试使用 boost::serialize 序列化一个简单的 std::unordered_map>。我设法保存地图就好了。当我尝试加载它时出现问题。 这是我编写的代码:
double E = 250E-4;
std::vector<double> init_vals(2,0.0);
std::unordered_map<double, std::vector<double>> map;
std::ofstream ofs("energy_map");
boost::archive::binary_oarchive oa(ofs);
map.emplace(E, init_vals);
oa << map;
我加载地图的代码是这样的:
std::ifstream ifs("energy_map");
std::unordered_map <double, std::vector<double>> map;
boost::archive::binary_iarchive ia(ifs);
ia >> map;
我得到 boost::archive::archive_exception 行 "ia >> map;"。异常是 "unregistered_cast" 异常,根据 boost 文档指出:
// base - derived relationship not registered with // void_cast_register I'm not quite sure why this isn't working out cause it seems fairly simple. (I've edited the code from my original files to make it simpler, but the lines are identical to that within my code).
感谢任何帮助。谢谢!
您很可能/还/序列化了其他一些多态类型。 SSCCE 示例显示这适用于 GCC:
http://paste.ubuntu.com/12963866/
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/unordered_map.hpp>
#include <boost/serialization/vector.hpp>
#include <fstream>
#include <iostream>
void write_map() {
std::unordered_map<double, std::vector<double> > map;
{
double E = 250E-4;
std::vector<double> init_vals(2, 0.0);
map.emplace(E, init_vals);
}
{
std::ofstream ofs("energy_map");
boost::archive::binary_oarchive oa(ofs);
oa << map;
}
}
void load_map() {
std::unordered_map<double, std::vector<double> > map;
{
std::ifstream ifs("energy_map");
boost::archive::binary_iarchive ia(ifs);
ia >> map;
}
for (auto &p : map) {
std::cout << p.first << " -> { ";
std::copy(p.second.begin(), p.second.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << "}\n";
}
}
int main() {
write_map();
load_map();
}
因此,如果您仍有问题,请查看
- 对于Undefined Behaviour
- 对于 ABI 问题(您是否使用与主程序相同的 compiler/library 版本和标志编译 boost)