Serialization boost多索引容器序列化
我试图将boost::multi_index_容器与boost::serialization一起使用。但是,当我使用指向对象的指针作为元素和非唯一的顺序时,加载序列化容器时会出现内存访问冲突。我发现有趣的是,对于唯一排序或使用对象而不是指针作为容器元素,不会发生错误 有人能告诉我我的代码是否有问题,或者这是boost库中的一个bug吗 以下是产生所述错误的最小示例:Serialization boost多索引容器序列化,serialization,boost,multi-index,Serialization,Boost,Multi Index,我试图将boost::multi_index_容器与boost::serialization一起使用。但是,当我使用指向对象的指针作为元素和非唯一的顺序时,加载序列化容器时会出现内存访问冲突。我发现有趣的是,对于唯一排序或使用对象而不是指针作为容器元素,不会发生错误 有人能告诉我我的代码是否有问题,或者这是boost库中的一个bug吗 以下是产生所述错误的最小示例: #include <boost/multi_index_container.hpp> #include <boo
#include <boost/multi_index_container.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/set.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <fstream>
using namespace std;
using namespace boost::multi_index;
struct element {
friend class boost::serialization::access;
std::string member1;
element( int num ) { member1 = boost::lexical_cast<string>( num ); }
element() {}
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & member1;
}
};
int main( int argc, const char *argv[] )
{
typedef multi_index_container<element *, indexed_by<ordered_non_unique<member<element, std::string, &element::member1>>>> TestSet;
TestSet myset;
srand( time (NULL ));
for (int i = 0; i < 20; i++) {
myset.insert(new element(rand()));
}
// Write set
ofstream os("test.bin");
boost::archive::binary_oarchive boa(os);
boa << myset;
os.close();
// Read set
TestSet newset;
ifstream is("test.bin");
boost::archive::binary_iarchive bia(is);
bia >> newset;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间boost::multi_索引;
结构元素{
好友类boost::serialization::access;
std::字符串成员1;
元素(int num){member1=boost::lexical_cast(num);}
元素(){}
模板
无效序列化(存档和ar,常量未签名整数版本)
{
应收账款&会员1;
}
};
int main(int argc,const char*argv[]
{
typedef多索引容器测试集;
测试集myset;
srand(时间(空));
对于(int i=0;i<20;i++){
插入(新元素(rand());
}
//写入集
流操作系统(“test.bin”);
boost::archive::binary_-oarchive-boa(操作系统);
boa>新闻集;
返回0;
}
流操作系统(“test.bin”)的代码>应为:
流操作系统的(“test.bin”,ios::binary)代码>
此外:
ifstream是(“test.bin”)代码>应该是:ifstream是(“test.bin”,ios::binary)代码>谢谢,这就是问题所在!