测试序列化

本文介绍了一个使用Boost库进行序列化操作的C++示例程序。该程序定义了几个类,并展示了如何将这些类的对象序列化到文件中,以及如何对包含多个对象的容器进行序列化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// SaveData.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
//#include "ToolBlock.h"
//#include "findLineOperator.h"
//
//int main()
//{
//    vector<BaseNode*> vec;
//
//
//    findLineOperator *base = new findLineOperator();
//    base->save();
//
//    vec.push_back(base);
//
//    return 0;
//}

// testSerialization.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>

#include <string>
#include <vector>
#include <iostream>
#include <sstream>  
#include <fstream>

using namespace std;

class BaseNode
{
public:
    std::string a="this is BaseNode a";
    std::string b="this is BaseNode b";
    //为使当前的结构能支持序列化,得加入下面的代码段
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & a;
        ar & b;
    }
};

class BaseOperator :public BaseNode
{
public:
    std::string c = "this is BaseOperator c";
    std::string d = "this is BaseOperator d";
    //为使当前的结构能支持序列化,得加入下面的代码段
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & c;
        ar & d;
    }
};
class findLineOperator :public BaseOperator {

public:
    int id;
    std::string strName;
    std::string strValue;

    findLineOperator() {}
    findLineOperator(int id, std::string strName, std::string strValue)
    {
        this->id = id;
        this->strName = strName;
        this->strValue = strValue;
    }

    

private:
    friend boost::serialization::access;          //声明友元,授予访问权限
    template<typename Archive>
    void serialize(Archive &ar, const unsigned int version)     //序列化函数
    {
        ar & id;
        ar & strName;
        ar & strValue;
        //ar & listMR;
    }
};

class ToolBlock
{
public:

    std::vector<BaseNode*> listMR;
    
};


int main(int argc, _TCHAR* argv[])
{
    std::stringstream ss;

    //序列化
    //findLineOperator p1(11, "anderson", "neo");      //被序列化的对象
    ToolBlock *tool = new ToolBlock();
    findLineOperator *mr = new findLineOperator(99,"eric","lili");
    findLineOperator *mr2=new findLineOperator();
    mr2->a = "this is b" ;
    mr2->b = "第二条记录";


    //压入对象
    tool->listMR.push_back(mr);
    tool->listMR.push_back(mr2);
                                                     
    //创建对象                                                 //为被序列化对象添加两条记录

    //对象初始化
    //mr->a = "apple";
    //mr->b = "第一条记录";


    //对象序列化
    string fileName = "save.cvteam";

    std::ofstream ofs;

    ofs.open(fileName.c_str(), ofstream::out);

    if (ofs)

    {

        boost::archive::text_oarchive oa(ofs);

        oa << tool->listMR;

    }

    ofs.close();

    //boost::archive::text_oarchive(ss) << p1; //序列化
                                             //序列化为xml形式要求中文为utf-8编码,否则打开文件是乱码
                                             //std::stringstream ss;
                                             //std::ofstream ofs("d:\\a.xml");
                                             //boost::archive::xml_oarchive oa(ofs);
                                             //oa << BOOST_SERIALIZATION_NVP(p1);


                                             //反序列化
    //MyData p2;                               //被反序列化的对象
    //boost::archive::text_iarchive(ss) >> p2; //反序列化

    //printTest(p2);
    //std::cout << "序列化后的=>" << ss.str() << std::endl;;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

究极调参工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值