C++ 2D矢量未正确保存并与boost::serialize库一起加载
我有一个C++ 2D矢量未正确保存并与boost::serialize库一起加载,c++,serialization,vector,boost,C++,Serialization,Vector,Boost,我有一个vector对象,我想从文件中保存和加载它: class Bar { private: std::vector<std::vector<char>> _map; friend class boost::serialization::access; template <class Archive> void serialize(Archive& ar, const unsigned int version)
vector
对象,我想从文件中保存和加载它:
class Bar {
private:
std::vector<std::vector<char>> _map;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
for(int i=0; i<_map.size(); ++i){
// ar & _map[i];
}
ar & _map;
}
};
这就是不同2D向量的外观。(右侧是顶部的一个:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 C 0 0 0 0
0 1 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
------------
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 C 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
0 1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
1 1 1 1 1 1 0 0 0 0
1 1 0 0 1 1 0 0 0 0
1 1 1 1 1 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0
你正在做一些你没有表现出来的错误:
#include <fstream>
#include <iostream>
#include <iomanip>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
using namespace std;
class Bar {
public:
void seed(std::vector<std::vector<char>> const& init) { _map = init; }
bool operator==(Bar const& other) const {
return _map == other._map;
}
private:
std::vector<std::vector<char>> _map;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int /*version*/) {
//for(size_t i=0; i<_map.size(); ++i){ ar & _map[i]; }
ar & _map;
}
};
int main()
{
std::vector<std::vector<char>> seed = {
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
{'0', '0', '0', '0', '0', 'C', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
};
// create and modify example object of class Bar here...
{
Bar s;
s.seed(seed);
std::ofstream ofs("map_save.save");
boost::archive::text_oarchive oa(ofs);
oa << s;
}
{
Bar news;
std::ifstream ifs("map_save.save");
boost::archive::text_iarchive ia(ifs);
ia >> news;
Bar compareTo;
compareTo.seed(seed);
std::cout << "Comparison: " << std::boolalpha << (compareTo == news) << "\n";
}
}*
为什么省略了无法编译的相关代码和post代码?我认为我的错误在于我使用boost序列化的方式。对于这个糟糕的示例,很抱歉,我尝试尽快更新我的问题!
#include <fstream>
#include <iostream>
#include <iomanip>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
using namespace std;
class Bar {
public:
void seed(std::vector<std::vector<char>> const& init) { _map = init; }
bool operator==(Bar const& other) const {
return _map == other._map;
}
private:
std::vector<std::vector<char>> _map;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int /*version*/) {
//for(size_t i=0; i<_map.size(); ++i){ ar & _map[i]; }
ar & _map;
}
};
int main()
{
std::vector<std::vector<char>> seed = {
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
{'0', '0', '0', '0', '0', 'C', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
};
// create and modify example object of class Bar here...
{
Bar s;
s.seed(seed);
std::ofstream ofs("map_save.save");
boost::archive::text_oarchive oa(ofs);
oa << s;
}
{
Bar news;
std::ifstream ifs("map_save.save");
boost::archive::text_iarchive ia(ifs);
ia >> news;
Bar compareTo;
compareTo.seed(seed);
std::cout << "Comparison: " << std::boolalpha << (compareTo == news) << "\n";
}
}*
Comparison: true