如何在Boost Serialization中检索多个对象值

How to retrieve multiple objects values in boost serialization

本文关键字:对象 检索 Boost Serialization      更新时间:2023-10-16

在下面的代码段中,我在'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;
     }