如何在Boost Serialization中检索多个对象值
How to retrieve multiple objects values in boost serialization
在下面的代码段中,我在'filename'文件中存储2个对象,但惊讶地看到在删除序列化期间第一个对象值检索2次。除此之外,我还想将多个类对象存储在同一文件中。目前,我能够将对象存储在文件中,但无法检索。
任何人都可以通过这一点亮起吗?
#include <fstream>
#include <iostream>
// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
using namespace std;
/////////////////////////////////////////////////////////////
// object_model
//
// illustrates serialization for a simple type
//
class object_model
{
private:
friend class boost::serialization::access;
// When the class Archive corresponds to an output archive, the
// & operator is defined similar to <<. Likewise, when the class Archive
// is a type of input archive the & operator is defined similar to >>.
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & classval;
ar & property;
}
string classval;
int property;
public:
object_model(){};
object_model(string d, int p) :
classval(d), property(p)
{}
string getClassval()
{
return classval;
}
int getproperty()
{
return property;
}
};
int main() {
// create and open a character archive for output
std::ofstream ofs("filename");
// create class instance
object_model g("lidar",10);
{
// save data to archive
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << g;
}
object_model g1("lidar1",20);
{
// save data to archive
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << g1;
}
// ... some time later restore the class instance to its orginal state
object_model newg;
{
// create and open an archive for input
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newg;
cout<<newg.getClassval();
cout<<newg.getproperty();
cout<<"done"<<endl;
}
// archive and stream closed when destructors are called
object_model newg1;
{
// create and open an archive for input
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newg1;
cout<<newg1.getClassval();
cout<<newg1.getproperty();
cout<<"done"<<endl;
}
return 0;
}
在保存数据到存档对象时,您需要重定向两个对象。它对我来说很好。
object_model g(" lidar",10(; object_model g1(" lidar1",20(;
{ //将数据保存到存档
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << g<<g1;
}
//...一段时间以后将类实例还原到其原始状态
object_model newg;
object_model newg1;
{
// create and open an archive for input
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newg>>newg1;
cout<<newg.getClassval();
cout<<newg.getproperty();
cout<<newg1.getClassval();
cout<<newg1.getproperty();
cout<<"done"<<endl;
}
相关文章:
- 如何检索由带通配符的字符串索引的对象
- 用于从可能不存在的容器中检索对象的 API 设计
- 在 c++ 中使用指向对象的指针检索数据
- 通过 std::bind 从对象成员检索值
- 将 JSON 对象作为参数在 JNI 中传递给 CPP,并在 CPP 中检索 JSON 对象的数据
- C++ 从树中检索对象
- 检索不同类型的对象指针
- 如何在Boost Serialization中检索多个对象值
- 使用Boost序列化保存和检索多个对象
- C++ 无法检索自定义矢量中的对象
- 如何通过先前存储的ID从结构对象中检索值?C
- 在另一个中检索JSON对象
- c++使用字符串句柄对对象进行通用存储和检索
- 从C 添加和检索类的对象
- 如何从wow64进程中检索特定内核对象的64位地址
- 检索在函数内部使用运算符new分配的类指针对象成员的值时出现的问题
- 从数据库中以 blob 数据类型形式存储的原始图像数据中检索 CImage 对象
- QML:如何检索默认字体对象
- 从继承的对象检索信息
- 从 win32api C++中的 LDAPMessage 对象检索'pre windows 2000 logon'名称