如何使用Boost序列化库序列化deques向量。
目前,我有以下代码用于此目的:
#include <boost/serialization/deque.hpp> //in includes
Item1(6128, std::deque<float> ()), //constructor initializations
Item2(6128, std::deque<float> ()),
Item3(6128,std::deque<int> ()),
Item4(6128,std::deque<int> ()),
private:
friend class boost::serialization::access;
template <typename Archive>
void
serialize(Archive &ar,
const unsigned int /*file_version*/)
{
for (unsigned i = 0; i < Item1.size(); ++i)
{ar & Item1[i];}
for (unsigned i = 0; i < Item2.size(); ++i)
{ar & Item2[i];}
for (unsigned i = 0; i < Item3.size(); ++i)
{ar & Item3[i];}
for (unsigned i = 0; i < Item4.size(); ++i)
{ar & Item4[i];}
}
std::vector< std::deque<float> > Item1, Item2;
std::vector< std::deque<int> > Item3, Item4;
此代码编译并运行正常。但是,我存储的值与序列化时加载的值不同。难道我做错了什么?这是序列化deques向量的正确方法吗?
感谢您的反馈。谢谢!
答案 0 :(得分:2)
您没有序列化Item1 ... Item4
的大小我建议您直接使用STL容器序列化:
e.g。
ar & Item1;
ar & Item2;
ar & Item3;
ar & Item4;
template<class Archive>
void save(Archive & ar, const unsigned int version) const
{
ar & Item1.size();
for (unsigned i = 0; i < Item1.size(); ++i)
{ar & Item1[i];}
ar & Item2.size();
for (unsigned i = 0; i < Item2.size(); ++i)
{ar & Item2[i];}
// etc...
}
template<class Archive>
void load(Archive & ar, const unsigned int version)
{
size_t size;
ar & size;
Item1.resize(size);
for (unsigned i = 0; i < Item1.size(); ++i)
{ar & Item1[i];}
ar & size;
Item2.resize(size);
for (unsigned i = 0; i < Item2.size(); ++i)
{ar & Item2[i];}
// etc...
}
BOOST_SERIALIZATION_SPLIT_MEMBER()