畅游C++ Boost Serialization 序列化

【BOOST C++ 19 应用库】(4) Boost.Serialization Boost.Serialization 有着丰富的内涵,本文就序列化的不同目的,给出详细解释:如归档、指针和引用、对象、封装。文中并给出足够多的示例代码。 阅读详情

原文链接:https://blog.csdn.net/qq2399431200/article/details/45621921

目录


1.工作环境

2.使用方法

3.工程下载

4.总结

5.参考文献

6.更新

7.C++ Boost::serialization简介


内容
1.工作环境

        VS2010 C++,Windows 7,Boost _1_55_0


2.使用方法

       1.到Boost官网下载对应操作系统平台下的Boost库,此库需要进行编译以生成“静态”和“共享”库。具体编译方法请上网搜索;

       2.包含解压后的头文件路径到工程中:VS2010|选择工程|属性|配置属性|VC++目录|

                              包含目录|添加“D:\C++\Boost\boost_1_55_0”    —— boost库头文件路径

                              库目录|添加“D:\C++\Boost\boost_1_55_0\stage\lib”  —— boost库.lib或.dll文件路径


2.1第一个简单的例子 —— Hello World ,将字符串内容归档到文本文件中

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdio>
    #include <boost/archive/text_oarchive.hpp>
    #include <boost/archive/text_iarchive.hpp>
     
    // 清单1.将字符串保存到文本归档文件中
    void save()
    {
        std::ofstream file("archive.txt");
        boost::archive::text_oarchive oa(file);
        std::string s = "Hello world\n";
        oa << s;  // oa & s; 清单3.使用&运算符执行“转储-恢复”操作
    }
     
    // 清单2.将字符串的内容加载到文本文件中
    void load()
    {
        std::ifstream file("archive.txt");
        boost::archive::text_iarchive ia(file);
        std::string s;
        ia >> s;  // ia & s; 清单3.使用&运算符执行“转储-恢复”操作
        std::cout << s << std::endl;
    }
     
    int main()
    {
        save();
        load();
        getchar();
    }


2.2从xml文档文件执行“转储-恢复”操作

    // 清单4
     
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdio>
    #include <boost/archive/xml_iarchive.hpp>
    #include <boost/archive/xml_oarchive.hpp>
     
    void save()
    {
        std::ofstream file("archive.xml");
        boost::archive::xml_oarchive oa(file);
        std::string s = "Hello world! 你好,世界!\n";
        //如果您想使用 XML 归档文件,而不是文本归档文件,需要将数据打包到一个名为 BOOST_SERIALIZATION_NVP 的宏中
        oa & BOOST_SERIALIZATION_NVP(s);  
    }
     
    void load()
    {
        std::ifstream file("archive.xml");
        boost::archive::xml_iarchive ia(file);
        std::string s;
        ia & BOOST_SERIALIZATION_NVP(s);
        std::cout << s << std::endl;
    }
     
    int main()
    {
        save();
        load();
        getchar();
    }

xml归档文件中的内容:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="10">
    <s>Hello world! 你好,世界!
    </s>
    </boost_serialization>


2.3对整数数组执行“转储-恢复”操作

    // 清单6
     
    #include <fstream>
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <cstdio>
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/archive/xml_iarchive.hpp>
     
    void save()
    {
        std::ofstream file("archive.xml");
        boost::archive::xml_oarchive oa(file);
        int arrary1[ ] = { 34, 78, 22, 1, 910 };
        oa & BOOST_SERIALIZATION_NVP(arrary1);
    }
     
    /*  是否可以仅通过指定指针 int* restored 完成此操作并为您恢复数组?
    答案是否定的。必须每次都指定大小。如果认真回答此问题的话,答案是对基本
    类型的指针进行序列化非常复杂。*/
    void load()
    {
        std::ifstream file("archive.xml");
        boost::archive::xml_iarchive ia(file);
        int restored[5];  //必须指定数组的大小
        ia & BOOST_SERIALIZATION_NVP(restored);
        std::ostream_iterator<int> oi(std::cout, " ");
        std::copy(restored, restored+5, oi);
    }
     
    int main()
    {
        save();
        load();
        getchar();
    }

xml归档文件中的内容:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="10">
    <arrary1>
        <count>5</count>
        <item>34</item>
        <item>78</item>
        <item>22</item>
        <item>1</item>
        <item>910</item>
    </arrary1>
    </boost_serialization>
     


2.4串行化STL集合

    // 清单8
     
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include <iterator>
    #include <cstdio>
    #include <boost/archive/xml_iarchive.hpp>
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/serialization/list.hpp>
    #include <boost/serialization/vector.hpp>
     
    void save( )
    {
        std::ofstream file("archive.xml");
        boost::archive::xml_oarchive oa(file);
        float array[ ] = {34.2, 78.1, 22.221, 1.0, -910.88};
        std::list<float> L1(array, array+5);
        std::vector<float> V1(array, array+5);
        oa & BOOST_SERIALIZATION_NVP(L1);
        oa & BOOST_SERIALIZATION_NVP(V1);
    }
     
    void load()
    {
        std::ifstream file("archive.xml");
        boost::archive::xml_iarchive ia(file);
        std::list<float> L2;
        ia >> BOOST_SERIALIZATION_NVP(L2);  //不需要指定范围/大小
     
        std::vector<float> V2;
        ia >> BOOST_SERIALIZATION_NVP(V2);  //不需要指定范围/大小
     
        std::ostream_iterator<float> oi(std::cout, " ");
        std::copy(L2.begin(), L2.end(), oi );
        std::copy(V2.begin(), V2.end(), oi );
    }
     
    int main()
    {
        save();
        load();
        getchar();
    }


xml归档文件中内容:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="10">
    <L1>
        <count>5</count>
        <item_version>0</item_version>
        <item>34.200001</item>
        <item>78.099998</item>
        <item>22.221001</item>
        <item>1</item>
        <item>-910.88</item>
    </L1>
    <V1>
        <count>5</count>
        <item_version>0</item_version>
        <item>34.200001</item>
        <item>78.099998</item>
        <item>22.221001</item>
        <item>1</item>
        <item>-910.88</item>
    </V1>
    </boost_serialization>


2.5序列化自己的类型——serialize方法的侵入版本

    // 清单10 所谓“侵入”即serialize方法写到类中
     
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/archive/xml_iarchive.hpp>
    #include <iostream>
    #include <fstream>
    #include <cstdio>
     
    namespace
    {
        using std::cout;
        using std::cin;
        using std::endl;
    }
     
    typedef struct date
    {
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
     
        date(int d, int m, int y):m_day(d), m_month(m) ,m_year(y) {  }
        date( ):m_day(1),m_month(1),m_year(2000) { }
        friend std::ostream& operator << (std::ostream& out, date& d)
        {
            out << "day:" << d.m_day << "month:" << d.m_month << "year:" << d.m_year;
            return out;
        }
        template<typename Archive>
        void serialize(Archive& archive, const unsigned int version)
        {
            archive & BOOST_SERIALIZATION_NVP(m_day);
            archive & BOOST_SERIALIZATION_NVP(m_month);
            archive & BOOST_SERIALIZATION_NVP(m_year);
        }
    }date;
     
    void save( )
    {
        std::ofstream file("archive.xml");
        boost::archive::xml_oarchive oa(file);
        date d(15, 8, 1947);
        oa & BOOST_SERIALIZATION_NVP(d);
    }
     
    void load( )
    {
        std::ifstream file("archive.xml");
        boost::archive::xml_iarchive ia(file);
        date dr;
        ia >> BOOST_SERIALIZATION_NVP(dr);
        std::cout << dr;
    }
     
    int main(void)
    {
        save();
        load();
        getchar();
    }

xml归档文件的内容:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="10">
    <d class_id="0" tracking_level="0" version="0">
        <m_day>15</m_day>
        <m_month>8</m_month>
        <m_year>1947</m_year>
    </d>
    </boost_serialization>


2.6序列化自己的类型——serialize方法的非侵入版本

    // 清单11 所谓“非侵入”即serialize方法不必写在类中、不属于类
     
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/archive/xml_iarchive.hpp>
    #include <iostream>
    #include <fstream>
    #include <cstdio>
     
    namespace
    {
        using std::cout;
        using std::cin;
        using std::endl;
    }
     
    typedef struct date
    {
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
     
        date(int d, int m, int y):m_day(d), m_month(m) ,m_year(y) {  }
        date( ):m_day(1),m_month(1),m_year(2000) { }
        friend std::ostream& operator << (std::ostream& out, date& d)
        {
            out << "day:" << d.m_day << "month:" << d.m_month << "year:" << d.m_year;
            return out;
        }
    }date;
     
    // 序列化相关的类和函数都属于boost::serialization命名空间里,所以自定义的serialize函数也可被其中的其它类和函数调用
    namespace boost
    {
        namespace serialization
        {
            template<typename Archive>
            void serialize(Archive& archive, date& d, const unsigned int version)
            {
                archive & BOOST_SERIALIZATION_NVP(d.m_day);
                archive & BOOST_SERIALIZATION_NVP(d.m_month);
                archive & BOOST_SERIALIZATION_NVP(d.m_year);
            }
        }
    }
     
     
    void save( )
    {
        std::ofstream file("archive.xml");
        boost::archive::xml_oarchive oa(file);
        date d(15, 8, 1947);
        oa & BOOST_SERIALIZATION_NVP(d);
    }
     
    void load( )
    {
        std::ifstream file("archive.xml");
        boost::archive::xml_iarchive ia(file);
        date dr;
        ia >> BOOST_SERIALIZATION_NVP(dr);
        std::cout << dr;
    }
     
    int main(void)
    {
        save();
        load();
        getchar();
    }


2.7通过基类指针转储派生类——使用xml文件归档

    // 清单13
     
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/archive/xml_iarchive.hpp>
    #include <iostream>
    #include <fstream>
    #include <cstdio>
    #include <string>
     
    namespace
    {
        using std::cout;
        using std::cin;
        using std::endl;
        using std::string;
    }
     
    class CBase
    {
        friend class boost::serialization::access;
        template<typename Archive>
        void serialize(Archive& ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(baseName); }
        string baseName;
    public:
        CBase( ) { baseName = "class CBase"; }
        virtual ~CBase( ) { }  //必须加一个virtual函数,否则“dynamic_cast<date*> (dr)”报error C2683: “dynamic_cast”:“CBase”不是多态类型 错误
                              //这是C++多态属性决定的
    };
     
     
    class date : public CBase
    {
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
    public:
        date(int d, int m, int y):m_day(d), m_month(m) ,m_year(y) {  }
        date( ):m_day(1),m_month(1),m_year(2000) { }
        friend std::ostream& operator << (std::ostream& out, date& d)
        {
            out << "day:" << d.m_day << "\nmonth:" << d.m_month << "\nyear:" << d.m_year;
            return out;
        }
        virtual ~date() { }
    private:
        friend class boost::serialization::access;
        template<typename Archive>
        void serialize(Archive& archive, const unsigned int version)
        {
            //archive & boost::serialization::base_object<CBase> (*this);  //用文本文档归档用此方法,当用于xml归档时会发生error C2664: “boost::mpl::assertion_failed”: 不能将参数 1 从“boost::mpl::failed ************boost::serialization::is_wrapper<T>::* ***********”转换为“boost::mpl::assert<false>::type”
            
                    archive & BOOST_SERIALIZATION_BASE_OBJECT_NVP(CBase);   //用xml归档时,需要将父类对象包装,即用此宏,否则发生如上错误提示
            
                    archive & BOOST_SERIALIZATION_NVP(m_day);
            archive & BOOST_SERIALIZATION_NVP(m_month);
            archive & BOOST_SERIALIZATION_NVP(m_year);
        }
    };
     
    void save( )
    {
        std::ofstream file("archive.xml");
        boost::archive::xml_oarchive oa(file);
        oa.register_type<date>();
        CBase *b = new date(16, 8, 1947);
        oa & BOOST_SERIALIZATION_NVP(b);
        delete b;
    }
     
    void load( )
    {
        std::ifstream file("archive.xml");
        boost::archive::xml_iarchive ia(file);
        ia.register_type<date>();
        CBase *dr;
        ia >> BOOST_SERIALIZATION_NVP(dr);
        date *dr2 = dynamic_cast<date*> (dr);
        std::cout << *dr2;
        delete dr2;
    }
     
    int main(void)
    {
        save();
        getchar();
        load();
        getchar();
    }

xml归档文件的内容:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="10">
    <b class_id="0" tracking_level="1" version="0" object_id="_0">
        <CBase class_id="1" tracking_level="1" version="0" object_id="_1">
            <baseName>class CBase</baseName>
        </CBase>
        <m_day>16</m_day>
        <m_month>8</m_month>
        <m_year>1947</m_year>
    </b>
    </boost_serialization>
     


2.8通过基类指针转储派生类——使用文本文件归档

    // 注意和“3.7”的区别
     
    #include <boost/archive/text_oarchive.hpp>
    #include <boost/archive/text_iarchive.hpp>
    #include <iostream>
    #include <fstream>
    #include <cstdio>
    #include <string>
     
    namespace
    {
        using std::cout;
        using std::cin;
        using std::endl;
        using std::string;
    }
     
    class CBase
    {
        friend class boost::serialization::access;
        template<typename Archive>
        void serialize(Archive& ar, const unsigned int version) { ar & baseName; }
        string baseName;
    public:
        CBase( ) { baseName = "class CBase"; }
        virtual ~CBase( ) { }  //必须加一个virtual函数,否则“dynamic_cast<date*> (dr)”报error C2683: “dynamic_cast”:“CBase”不是多态类型 错误
        //这是C++多态属性决定的
    };
     
     
    class date : public CBase
    {
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
    public:
        date(int d, int m, int y):m_day(d), m_month(m) ,m_year(y) {  }
        date( ):m_day(1),m_month(1),m_year(2000) { }
        friend std::ostream& operator << (std::ostream& out, date& d)
        {
            out << "day:" << d.m_day << "\nmonth:" << d.m_month << "\nyear:" << d.m_year;
            return out;
        }
        virtual ~date() { }
    private:
        friend class boost::serialization::access;
        template<typename Archive>
        void serialize(Archive& archive, const unsigned int version)
        {
            archive & boost::serialization::base_object<CBase> (*this);  //用文本文档归档用此方法,当用于xml归档时会发生error C2664: “boost::mpl::assertion_failed”: 不能将参数 1 从“boost::mpl::failed ************boost::serialization::is_wrapper<T>::* ***********”转换为“boost::mpl::assert<false>::type”
            //archive & BOOST_SERIALIZATION_BASE_OBJECT_NVP(CBase);   //用xml归档时,需要将父类对象包装,即用此宏,否则发生如上错误提示
            archive & m_day;
            archive & m_month;
            archive & m_year;
        }
    };
     
    void save( )
    {
        std::ofstream file("archive.txt");
        boost::archive::text_oarchive oa(file);
        oa.register_type<date>();
        CBase *b = new date(16, 8, 1947);
        oa & b;
        delete b;
    }
     
    void load( )
    {
        std::ifstream file("archive.txt");
        boost::archive::text_iarchive ia(file);
        ia.register_type<date>();
        CBase *dr;
        ia >> dr;
        date *dr2 = dynamic_cast<date*> (dr);
        std::cout << *dr2;
        delete dr2;
    }
     
    int main(void)
    {
        save();
        getchar();
        load();
        getchar();
    }

文本文件归档内容:

22 serialization::archive 10 0 1 0
0 1 0
1 11 class CBase 16 8 1947


2.9使用指针执行“转储-恢复”操作

    // 清单15
     
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/archive/xml_iarchive.hpp>
    #include <iostream>
    #include <fstream>
    #include <cstdio>
     
    namespace
    {
        using std::cout;
        using std::cin;
        using std::endl;
    }
     
    typedef struct date
    {
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
     
        date(int d, int m, int y):m_day(d), m_month(m) ,m_year(y) {  }
        date( ):m_day(1),m_month(1),m_year(2000) { }
        friend std::ostream& operator << (std::ostream& out, date& d)
        {
            out << "day:" << d.m_day << "month:" << d.m_month << "year:" << d.m_year;
            return out;
        }
        template<typename Archive>
        void serialize(Archive& archive, const unsigned int version)
        {
            archive & BOOST_SERIALIZATION_NVP(m_day);
            archive & BOOST_SERIALIZATION_NVP(m_month);
            archive & BOOST_SERIALIZATION_NVP(m_year);
        }
    }date;
     
    void save( )
    {
        std::ofstream file("archive.xml");
        boost::archive::xml_oarchive oa(file);
        date *d = new date(15, 8, 1947);
        cout << d << endl;
        oa & BOOST_SERIALIZATION_NVP(d);
    }
     
    void load( )
    {
        std::ifstream file("archive.xml");
        boost::archive::xml_iarchive ia(file);
        date *dr;
        ia >> BOOST_SERIALIZATION_NVP(dr);
        std::cout << dr << endl;
        cout << *dr;
    }
     
    int main(void)
    {
        save();
        load();
        getchar();
    }

运行结果:

0047A108
0047A6B8
day:15month:8year:1947

xml归档文件中的内容:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="10">
    <d class_id="0" tracking_level="1" version="0" object_id="_0">
        <m_day>15</m_day>
        <m_month>8</m_month>
        <m_year>1947</m_year>
    </d>
    </boost_serialization>


2.10使用指针执行“转储-恢复”操作——将两个指针转储到同一个对象

    // 清单17
     
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/archive/xml_iarchive.hpp>
    #include <iostream>
    #include <fstream>
    #include <cstdio>
     
    namespace
    {
        using std::cout;
        using std::cin;
        using std::endl;
    }
     
    typedef struct date
    {
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
     
        date(int d, int m, int y):m_day(d), m_month(m) ,m_year(y) {  }
        date( ):m_day(1),m_month(1),m_year(2000) { }
        friend std::ostream& operator << (std::ostream& out, date& d)
        {
            out << "day:" << d.m_day << "month:" << d.m_month << "year:" << d.m_year;
            return out;
        }
        template<typename Archive>
        void serialize(Archive& archive, const unsigned int version)
        {
            archive & BOOST_SERIALIZATION_NVP(m_day);
            archive & BOOST_SERIALIZATION_NVP(m_month);
            archive & BOOST_SERIALIZATION_NVP(m_year);
        }
    }date;
     
    void save()
    {
        std::ofstream file("archive.xml");
        boost::archive::xml_oarchive oa(file);
        date *d = new date(15,8,1947);
        cout << d << endl;
        oa & BOOST_SERIALIZATION_NVP(d);
        date *d2 = d;
        oa & BOOST_SERIALIZATION_NVP(d2);
    }
     
    void load()
    {
        std::ifstream file("archive.xml");
        boost::archive::xml_iarchive ia(file);
        date *dr;
        ia >> BOOST_SERIALIZATION_NVP(dr);
        cout << dr << endl;
        cout << *dr << endl;
        date *dr2;
        ia >> BOOST_SERIALIZATION_NVP(dr2);
        cout << dr2 << endl;
        cout << *dr2;
    }
     
    int main(void)
    {
        save();
        load();
        getchar();
    }

运行结果:

0010A108
0010A6B8
day:15month:8year:1947
0010A6B8
day:15month:8year:1947

xml归档文件中的内容:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="10">
    <d class_id="0" tracking_level="1" version="0" object_id="_0">
        <m_day>15</m_day>
        <m_month>8</m_month>
        <m_year>1947</m_year>
    </d>
    <d2 class_id_reference="0" object_id_reference="_0"></d2>
    </boost_serialization>
     

2.11包含作为d的引用d2的归档文件

    // 清单19  和参考网页(IBM)中的那部分不一致
     
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/archive/xml_iarchive.hpp>
    #include <iostream>
    #include <fstream>
    #include <cstdio>
     
    namespace
    {
        using std::cout;
        using std::cin;
        using std::endl;
    }
     
    typedef struct date
    {
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
     
        date(int d, int m, int y):m_day(d), m_month(m) ,m_year(y) {  }
        date( ):m_day(1),m_month(1),m_year(2000) { }
        friend std::ostream& operator << (std::ostream& out, date& d)
        {
            out << "day:" << d.m_day << "month:" << d.m_month << "year:" << d.m_year;
            return out;
        }
        template<typename Archive>
        void serialize(Archive& archive, const unsigned int version)
        {
            archive & BOOST_SERIALIZATION_NVP(m_day);
            archive & BOOST_SERIALIZATION_NVP(m_month);
            archive & BOOST_SERIALIZATION_NVP(m_year);
        }
    }date;
     
    void save()
    {
        std::ofstream file("archive.xml");
        boost::archive::xml_oarchive oa(file);
        date *d = new date(15,8,1947);
        cout << d << endl;
        oa & BOOST_SERIALIZATION_NVP(d);
        date* &d2 = d;     //d2 reference d
        oa & BOOST_SERIALIZATION_NVP(d2);  
    }
     
    void load()
    {
        std::ifstream file("archive.xml");
        boost::archive::xml_iarchive ia(file);
        date *dr;
        ia >> BOOST_SERIALIZATION_NVP(dr);
        cout << dr << endl;
        cout << *dr << endl;
        date *dr2;
        ia >> BOOST_SERIALIZATION_NVP(dr2);
        cout << dr2 << endl;
        cout << *dr2;
    }
     
    int main(void)
    {
        save();
        load();
        getchar();
    }

xml归档文件的内容:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="10">
    <d class_id="0" tracking_level="1" version="0" object_id="_0">
        <m_day>15</m_day>
        <m_month>8</m_month>
        <m_year>1947</m_year>
    </d>
    <d2 class_id_reference="0" object_id_reference="_0"></d2>
    </boost_serialization>

2.12将serialize拆分成save和load

        有时候,您不想使用同样的 serialize 方法来转储和恢复对象。在这种情况下,您可以将 serialize 方法拆分成两个方法,即save 和load,它们具有类似的签名。这两个方法都是之前定义的serialize 方法的一部分。此外,需要添加BOOST_SERIALIZATION_SPLIT_MEMBER 宏作为类定义的一部分。(引用参考文献[1])

    // 清单21
     
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/archive/xml_iarchive.hpp>
    #include <iostream>
    #include <fstream>
    #include <cstdio>
     
    namespace
    {
        using std::cout;
        using std::cin;
        using std::endl;
    }
     
    typedef struct date
    {
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
     
        date(int d, int m, int y):m_day(d), m_month(m) ,m_year(y) {  }
        date( ):m_day(1),m_month(1),m_year(2000) { }
        friend std::ostream& operator << (std::ostream& out, date& d)
        {
            out << "day:" << d.m_day << "month:" << d.m_month << "year:" << d.m_year;
            return out;
        }
    //     template<typename Archive>
    //     void serialize(Archive& archive, const unsigned int version)
    //     {
    //         archive & BOOST_SERIALIZATION_NVP(m_day);
    //         archive & BOOST_SERIALIZATION_NVP(m_month);
    //         archive & BOOST_SERIALIZATION_NVP(m_year);
    //     }
            //将serialize函数分割成save和load函数,两个者实现的效果是等价的
        template<class Archive>
        void save(Archive& archive, const unsigned int version) const //注意 save 方法签名后的 const。如果没有 const 限定符,该代码将无法编译
        {
            archive << BOOST_SERIALIZATION_NVP(m_day);
            archive << BOOST_SERIALIZATION_NVP(m_month);
            archive << BOOST_SERIALIZATION_NVP(m_year);
        }
     
        template<class Archive>
        void load(Archive& archive, const unsigned int version)
        {
            archive >> BOOST_SERIALIZATION_NVP(m_day);
            archive >> BOOST_SERIALIZATION_NVP(m_month);
            archive >> BOOST_SERIALIZATION_NVP(m_year);
        }
        BOOST_SERIALIZATION_SPLIT_MEMBER( ) // must be part of class
    }date;
     
    void save( )
    {
        std::ofstream file("archive.xml");
        boost::archive::xml_oarchive oa(file);
        date d(15, 8, 1991);
        oa & BOOST_SERIALIZATION_NVP(d);
    }
     
    void load( )
    {
        std::ifstream file("archive.xml");
        boost::archive::xml_iarchive ia(file);
        date dr;
        ia >> BOOST_SERIALIZATION_NVP(dr);
        std::cout << dr;
    }
     
    int main(void)
    {
        save();
        load();
        getchar();
    }

2.13了解Boost::serialize的版本控制

    serialize、save 和 load 的方法签名都使用无符号整数版本作为最后一个参数。这些数字有什么用?随着时间变化,类的内部变量名称可能发生变化,添加新的字段或移除已有字段,等等。这是软件开发过程中的自然进程,除了归档文件仍然保存着关于数据类型原有状态的信息。为了规避这个问题,需要使用版本号。(引用参考文献[1])

        在date类的基础上增加一个unsigned int  ver变量用于保存当前类Mydate的版本号。

    // 清单22
     
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/archive/xml_iarchive.hpp>
    #include <iostream>
    #include <fstream>
    #include <cstdio>
     
    namespace
    {
        using std::cout;
        using std::cin;
        using std::endl;
    }
     
    typedef struct date
    {
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
     
        date(int d, int m, int y):m_day(d), m_month(m) ,m_year(y) {  }
        date( ):m_day(1),m_month(1),m_year(2000) { }
        friend std::ostream& operator << (std::ostream& out, date& d)
        {
            out << "day:" << d.m_day << "month:" << d.m_month << "year:" << d.m_year << endl;
            return out;
        }
        template<typename Archive>
        void serialize(Archive& archive, const unsigned int version)
        {
            archive & BOOST_SERIALIZATION_NVP(m_day);
            archive & BOOST_SERIALIZATION_NVP(m_month);
            archive & BOOST_SERIALIZATION_NVP(m_year);
        }
    }date;
    BOOST_CLASS_VERSION(date,0);
     
    // Mydate
    typedef struct Mydate
    {
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
        unsigned int ver;  //版本
     
        Mydate(int d, int m, int y, int version):m_day(d), m_month(m) ,m_year(y), ver(version) {  }
        Mydate( ):m_day(1),m_month(1),m_year(2000),ver(1) { }
        friend std::ostream& operator << (std::ostream& out, Mydate& d)
        {
            out << "day:" << d.m_day << "month:" << d.m_month << "year:" << d.m_year << " -- Version:" << d.ver << endl;
            return out;
        }
        template<typename Archive>
        void serialize(Archive& archive, const unsigned int version)
        {
            archive & BOOST_SERIALIZATION_NVP(m_day);
            archive & BOOST_SERIALIZATION_NVP(m_month);
            archive & BOOST_SERIALIZATION_NVP(m_year);
            if(version == 1)
            {
                archive & BOOST_SERIALIZATION_NVP(ver);
            }
        }
    }Mydate;
    BOOST_CLASS_VERSION(Mydate,1);
     
    void save( )
    {
        std::ofstream file("version.xml");
        boost::archive::xml_oarchive oa(file);
        date d(15, 8, 1947);
        oa & BOOST_SERIALIZATION_NVP(d);
        Mydate myd(1, 1, 1991, 1);
        oa & BOOST_SERIALIZATION_NVP(myd);
    }
     
    void load( )
    {
        std::ifstream file("version.xml");
        boost::archive::xml_iarchive ia(file);
        date dr;
        ia >> BOOST_SERIALIZATION_NVP(dr);
        Mydate myd;
        ia >> BOOST_SERIALIZATION_NVP(myd);
        std::cout << dr;
        std::cout << myd;
    }
     
    int main(void)
    {
        save();
        load();
        getchar();
    }

运行结果:

day:15month:8year:1947
day:1month:1year:1991 -- Version:1

xml归档文件中内容

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="10">
    <d class_id="0" tracking_level="0" version="0">
        <m_day>15</m_day>
        <m_month>8</m_month>
        <m_year>1947</m_year>
    </d>
    <myd class_id="1" tracking_level="0" version="1">
        <m_day>1</m_day>
        <m_month>1</m_month>
        <m_year>1991</m_year>
        <ver>1</ver>
    </myd>
    </boost_serialization>
     

2.14 使用共享指针boost::shared_ptr执行“转储-恢复”操作

    // 清单23
     
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdio>
    #include <boost/archive/xml_iarchive.hpp>
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/serialization/shared_ptr.hpp>  //缺此头文件 —— error C2039: “serialize”: 不是“boost::shared_ptr<T>”的成员    d:\c++\boost\boost_1_55_0\boost\serialization\access.hpp
     
     
    typedef struct date
    {
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
     
        date(int d, int m, int y):m_day(d), m_month(m) ,m_year(y) {  }
        date( ):m_day(1),m_month(1),m_year(2000) { }
        friend std::ostream& operator << (std::ostream& out, date& d)
        {
            out << "day:" << d.m_day << "month:" << d.m_month << "year:" << d.m_year;
            return out;
        }
        template<typename Archive>
        void serialize(Archive& archive, const unsigned int version)
        {
            archive & BOOST_SERIALIZATION_NVP(m_day);
            archive & BOOST_SERIALIZATION_NVP(m_month);
            archive & BOOST_SERIALIZATION_NVP(m_year);
        }
    }date;
     
    void save()
    {
        std::ofstream file("shared_ptr.xml");
        boost::archive::xml_oarchive oa(file);
        boost::shared_ptr<date> d (new date(15, 8, 1947));
        oa & BOOST_SERIALIZATION_NVP(d);
        // other code bellow
    }
     
    void load()
    {
        std::fstream file("shared_ptr.xml");
        boost::archive::xml_iarchive ia(file);
        boost::shared_ptr<date> dr;
        ia & BOOST_SERIALIZATION_NVP(dr);
        std::cout << *dr;
    }
     
    int main( )
    {
        save();
        load();
        getchar();
    }

xml文档中的内容:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="10">
    <d class_id="0" tracking_level="0" version="1">
        <px class_id="1" tracking_level="1" version="0" object_id="_0">
            <m_day>15</m_day>
            <m_month>8</m_month>
            <m_year>1947</m_year>
        </px>
    </d>
    </boost_serialization>
     


2.15 指向栈对象的指针需要在实际对象之后转储

    // 清单24
     
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdio>
    #include <boost/archive/xml_iarchive.hpp>
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/serialization/shared_ptr.hpp>  //缺此头文件 —— error C2039: “serialize”: 不是“boost::shared_ptr<T>”的成员    d:\c++\boost\boost_1_55_0\boost\serialization\access.hpp
     
     
    typedef struct date
    {
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
     
        date(int d, int m, int y):m_day(d), m_month(m) ,m_year(y) {  }
        date( ):m_day(1),m_month(1),m_year(2000) { }
        friend std::ostream& operator << (std::ostream& out, date& d)
        {
            out << "day:" << d.m_day << "month:" << d.m_month << "year:" << d.m_year;
            return out;
        }
        template<typename Archive>
        void serialize(Archive& archive, const unsigned int version)
        {
            archive & BOOST_SERIALIZATION_NVP(m_day);
            archive & BOOST_SERIALIZATION_NVP(m_month);
            archive & BOOST_SERIALIZATION_NVP(m_year);
        }
    }date;
     
    void save()
    {
        std::ofstream file("stackobject.xml");
        boost::archive::xml_oarchive oa(file);
        date d(1,1,1991);
        date *d2 = &d;
        oa & BOOST_SERIALIZATION_NVP(d);  
        oa & BOOST_SERIALIZATION_NVP(d2);  //指向栈对象的指针需要在实际对象之后转储
    }
     
    void load()
    {
        std::fstream file("stackobject.xml");
        boost::archive::xml_iarchive ia(file);
        date dr;
        ia & BOOST_SERIALIZATION_NVP(dr);
        std::cout << dr << std::endl;
        date *dr2;
        ia & BOOST_SERIALIZATION_NVP(dr2);
        std::cout << *dr2 << std::endl;
    }
     
    int main( )
    {
        save();
        load();
        getchar();
    }


xml文档中的内容:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="10">
    <d class_id="0" tracking_level="1" version="0" object_id="_0">
    <m_day>1</m_day>
    <m_month>1</m_month>
    <m_year>1991</m_year>
    </d>
    <d2 class_id_reference="0" object_id_reference="_0"></d2>
    </boost_serialization>

           如果有多个指针指向同一个对象,Serialization 会使用 class_id_reference 将指针与原对象关联起来(每个对象都有一个唯一的类 ID)。原对象的每个后续指针都会将 object_id_reference 改成 _1、_2,以此类推。


3.工程下载

        整个工程的下载地址,点此处。


4.总结

5.参考文献

[1]. 《Boost Serialization 库》 http://www.ibm.com/developerworks/cn/aix/library/au-boostserialization/


6.更新

2015-05-10  第一次更新   全文开始,增加2.1~2.7 部分内容

2015-05-11  第二次更新    增加2.8~2.13部分内容

2015-05-14  第三次更新    增加2.14~2.15部分内容

2015-05-19  第四次更新    提供工程下载


7.C++ Boost::serialization简介

以下内容引用Boost库的“libs/serialization/doc/index.html”页面:    

         Here, we use the term "serialization" to meanthe reversible deconstruction of an arbitrary set of C++ data structuresto a sequence of bytes. Such a system can be used to reconstitutean equivalent structure in another program context. Depending onthe context, this might used implement object persistence, remoteparameter passing or other facility. In this system we use the term"archive" to refer to a specific rendering of thisstream of bytes. This could be a file of binary data, text data, XML, or some other created by the user of this library.
Our goals for such a system are:

    Code portability - depend only on ANSI C++ facilities.
    Code economy - exploit features of C++ such as RTTI, templates, and multiple inheritance, etc. where appropriate to make code shorter and simpler to use.
    Independent versioning for each class definition. That is, when a class definition changed, older files can still be imported to the new version of the class.
    Deep pointer save and restore. That is, save and restore of pointers saves and restores the data pointed to.
    Proper restoration of pointers to shared data.
    Serialization of STL containers and other commonly used templates.
    Data Portability - Streams of bytes created on one platform should be readable on any other.
    Orthogonal specification of class serialization and archive format. That is, any file format should be able to store serialization of any arbitrary set of C++ data structures without having to alter the serialization of any class.
    Non-intrusive. Permit serialization to be applied to unaltered classes. That is, don't require that classes to be serialized be derived from a specific base class or implement specified member functions. This is necessary to easily permit serialization to be applied to classes from class libraries that we cannot or don't want to have to alter.
    The archive interface must be simple enough to easily permit creation of a new type of archive.
    The archive interface must be rich enough to permit the creation of anarchive that presents serialized data as XML in a useful manner.

Other implementations
Before getting started I searched around for current implementations. I found several.

    MFC This is the one that I am very familiar with. I have used it for several years and have found it very useful. However it fails requirements 1, 2, 3, 6, 7, and 9. In spite of all the requirements not fulfilled, this is the most useful implementation I've found. It turns out that class versioning - partially implemented in MFC - really is indispensable for my applications. Inevitably, version 1.x of a shipping program needs to store more information in files than was originally provided for. MFC is the only one of these implementations that supports this - though only for the most derived class. Still it's better than nothing and does the job. MFC doesn't implement serialization of STL collections. Though it does so for MFC collections.
    CommonC++ libraries [1] As far as I can tell, this closely follows the MFC implementation but does address a few of the issues. It is portable and creates portable archives but skips versioning. It does support proper and complete restoration of pointers and STL collections. It does address compression though not in the way that I would prefer. The package would also benefit from having better documentation. So it fails to address 2, 3, 7, 8, and 9.
    Eternity [2] This is a bare bones package. It seems well coded but it really needs documentation and examples. It's not obvious how to use it without time consuming study of the source code. Recent versions do support files in XML format. This Fails 3, 6, 7?, 8, and 9.
    Holub's implementation [3] This is the article that first got me thinking about my own requirements for a serialization implementation. Interesting and worth the read if you can overlook the arrogant tone of the prose. This implementation fails 2, 3, 4, 5, and 6.
    s11n [13] This library has similar goals to this one. Some aspects of the implemenation are also similar. As of this writing, it would seem that:
        Portability(1) is guarenteed only for recent versions of GCC.
        Versioning(3) of class definitions is not explicitly supported by the library.
        it doesn't seem to automatically account for shared pointers(5). I concluded this from the documentation as well as the statement that serialization of graph like structures is not supported.
    Its has lots of differences - and lots in common with this implementation.

基于boost的序列化与反序列化 1、包含文件和二进制两种序列化的方式 2、支持类的序列化和STL容器的序列化 3、支持序列化的流自定义(示例中使用的是std::stringstream,也可以使用其他的流) 立即下载

相关推荐

电工识图入门:从电气符号到控制逻辑的完整指南

电工图纸是电气工程领域的通用语言,无论是设备安装、线路检修还是系统设计,看不懂图纸就像航海没有海图。很多初学者面对复杂的电气符号和线路走向感到无从下手,实际上只要掌握正确的识图方法,就能从简单的照明电路逐步过渡到复杂的自动化控制系统。 本文将从最基础的电气符号和图纸类型讲起,通过实际图纸案例解析,带你建立完整的识图逻辑。你将学会如何区分主回路和控制回路,如何追踪电流路径,以及如何结合图纸进行故障排查。无论你是电工新手、设备维护人员还是相关专业学生,这套方法都能帮你快速提升图纸理解能力。 1. 电气图纸基础:

chengyixian7877的博客 343

C++Boost的序列化

date;侵入式版本{}{}return out;} date;date dr;序列化版本控制123public:CPerson();;~CPerson();private://要想序列化用户定义的类型 必须定义seriallze函数//serialzation不应该被显示调用 因为它仅在序列化或者反序列化的时候调用。

LuckyStar的博客 2198

【实用教程】使用AlphaFold2进行蛋白质结构在线预测

使用Colab进行简单快速的AlphaFold2在线的蛋白质三维结构预测

Eumenidus的博客 14万+

boost::serialization 多态(polymorphic)序列化技巧及分析

假如某段程序中我们只记录了一些虚基类指针对象,

GIS攻城狮老武 3189

C#上位机玩转OPC UA:从服务器搭建到数据订阅(西门子S7-1200/欧姆龙CP1E全案例)

服务器搭建:C#可快速搭建OPC UA服务器,适配无原生OPC UA的PLC(如欧姆龙CP1E),统一数据交互接口;客户端开发:核心实现“自动重连、批量订阅、异步处理”,满足工业场景7×24小时稳定运行需求;PLC对接:西门子S7-1200可直接对接原生OPC UA,欧姆龙CP1E需通过网关中转,重点注意地址格式与通信参数匹配。本文提供的代码已在3C产线、自动化流水线等场景落地验证,可直接复用核心模块,仅需根据实际PLC型号调整节点映射与通信参数。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 769

超临界二氧化碳发电技术深度解析

超临界二氧化碳发电技术正在突破传统热电转换的物理边界。作为新一代发

硬核科技 606

boost_archive

boost_archive0.引言1.demo2.Result 0.引言 用了都说好:D 1.demo ArchiveTest.cpp #include <fstream> #include <iostream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/serial

fb_941219的博客 931

Vue3 - Vite运行打包时Uncaught ReferenceError: process is not defined(解决vue3+vite报错process is not defined)

vue3,vite,vue3+vite报错,process,UncaughtReferenceError:processisnotdefined,浏览器控制台报错processisnotdefined解决方法,vue3如何解决报错,vite报错怎么办,前端打包UncaughtReferenceError:processisnotdefined,npmrunbuild打包报错,vite打包时出现错误,vue3打包运行找不到process,热更新会报错UncaughtRefer

🏆 前端领域优质创作者 7743

boost生成xml与tinyxml之间的格式转换

相关链接:畅游C++ Boost Serialization 序列化 本博客将普通xml转成boost可读;     之前做项目,发现一个问题就是用boost生成的xml与tinyxml生成的xml格式不同,具体大概为这样:tinyxml与boost都是处理xml的工具:下图为tinyxml与boost读一个boost生成xml后重新生成的xml     (这里想展示的是格式上区别,其实还有点不同的是,就是科学计数法的方式,bo

two_apples的博客 459

Android Audio最全面试题及参考答案(7万字长文)

在 Android 音频系统中,应用通过框架层的 API 发出音频请求,这些请求经过音频服务层的处理,传递到 HAL 层,再由 HAL 层调用音频驱动层与硬件进行交互,最终实现音频的播放、录制等功能。在 Android 音频系统中,应用通过框架层的 API 发出音频请求,这些请求经过音频服务层的处理,传递到 HAL 层,再由 HAL 层调用音频驱动层与硬件进行交互,最终实现音频的播放、录制等功能。音频播放完成事件:当使用 MediaPlayer 等音频播放类播放音频时,音频播放完成后会触发这个事件。

大模型大数据攻城狮的专栏 2614

YOLOv11与3D点云检测融合技术深度解析:从2D检测到三维空间投影的完整实现

2D-3D融合目标检测技术研究 本文系统阐述了基于YOLOv11的2D-3D目标检测融合技术,重点分析了YOLOv11在融合系统中的定位优势及其架构设计。研究表明,2D图像与3D点云数据具有显著的互补特性:2D图像提供丰富语义信息,而3D点云则精确描述几何结构。 关键技术要点: YOLOv11通过GSConv平衡卷积和VoVGSCS特征融合模块,在保持实时性的同时提升了检测精度 系统采用模块化设计,包含坐标投影、点云分割、边界框拟合等核心组件 针对点云数据的非均匀分布特性,提出了多尺度特征增强和距离差异化检

本博客聚焦 YOLOv11 全流程落地,涵盖架构优化、数据集处理、训练技巧与多场景部署,兼及国产数据库、Java 开发与 AI 模型应用,内容兼顾理论与工程实践,为开发者提供系统干货,助力高效提升技术能力。 1134

boost序列化补充说明

boost序列化补充说明昨天研究了boost::xml_serialization的序列化的应用部份,特别是在xml_serialization中关于对象序列化的部份还是值得研究一下的,我花了点时间看了看这部份的代码是如何实现的,这里记录一下,做个笔记和朋友们分享(一)回顾昨天研究到对象序列化部份可以通过如下的方式做对象的序列化test* a = NULL;oa >

conyzhou的开发手记 2567

主题033:燃烧反应工程

燃烧反应工程(Combustion Reaction Engineering)是研究燃料与氧化剂发生剧烈氧化反应的科学,涉及化学反应动力学、流体力学、传热传质等多个学科。燃烧是人类利用能源的主要方式,广泛应用于发电、交通运输、工业加热等领域。燃烧反应通常通过自由基链式反应进行。链引发:RH+O2⟶R⋅+HO2⋅ RH + O_2 \longrightarrow R^\cdot + HO_2^\cdot RH+O2​⟶R⋅+HO2⋅​链传递:R⋅+O2⟶RO2⋅ R^\cdot + O_2 \longrigh

kkchenjj的博客 235

boost 序列化_Boost序列化库

长时间燃烧午夜机油后开发的软件在客户现场崩溃。 您无所事事,因为没有可用的测试用例来帮助您复制崩溃并调试出了什么问题。 这是许多人熟悉的情况,但是不断出现的问题是,对此可以做些什么? 仅转储堆栈跟踪信息并不是最佳选择。 您需要深入了解代码的数据结构以检查其值。 解决方案是Boost序列化库。 您可以使用它将程序内容转储到归档文件(文本或XML文件)中,并从同一归档文件中还原数据,以在崩溃之...

cusi77914的博客 1129

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

boost::serialization 序列化库详解

boost::serialize中文教程

m0_46268825的博客 3039

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

boost -- 序列化(serialize)

本篇文字主要来记录学习使用boost进行序列化 介绍 实例 序列化成普通文本 非侵入式 // C #include <stdlib.h> #include <stdio.h> // C++ #include <fstream> #include <string> // boost #include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchi

There is an octopus in the fish pond 2330

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

序列化方案总结

1、什么是序列化 程序员在编写应用程序的时候往往需要将程序的某些数据存储在内存中,然后将其写入某个文件或是将它传输到网络中的另一台计算机上以实现通讯。这个将程序数据转化成能被存储并传输的格式的过程被称为“序列化”(Serialization),而它的逆过程则可被称为“反序列化” (Deserialization)。 简单来说,序列化就是将对象实例的状态转换为可保持或传输的格式的过程。与序列化相对的...

Jiewang的博客 1116

C++中使用boost::serialization库――应用篇

  C++中使用boost::serialization库――应用篇作者: 刘凯 mslk.sa@gmail.com 概要:         本文先简述在项目中使用boost::serialization库的两种实现方式:一种是成员函数模板,另一种是友元函数模板。之后涉及使用库的两种实现方式的优劣比较、使用时的代码模板示例,通过比较得出结论应以第二种实现方法为主,另外,因为bo

mslk的专栏 1万+

Boost中支持序列化反序列化的库--boost.serialization

boost.serialization的特点 1. 支持XML和二进制两种形式 2. 支持类的版本号 3. 支持深度指针存储 4. 支持多个指针共享数据 5. 支持STL容器存储 6. 支持原生数组存储 7. 支持非侵入性的设计,序列化的类不需要继承特殊的类,不需要写特殊的方

记事本 3223

C++使用boost.serialization序列化与反序列化

侵入式序列化,非侵入式序列化 虽然看着有些长,但是请各位要注意,这是两个例子,分别介绍基本的普通类序列化与子类序列化,并反序列化 反序列化的代码其实都一样(只是把对象A改成了B)修改了构造函数传参。 特别关注 template void serialize(Archive& ar, const unsigned int version) 我们在后面都会用到 序列化STL容器

a4364634611的博客 1723

【C/C++ 序列化 】深入探索 Boost 序列化:从理论到实践的全面指南

在现代软件开发中,数据的持久化和跨平台通信是两个核心问题。其中,数据序列化(Data Serialization)扮演着至关重要的角色。Boost 序列化库(Boost Serialization Library)为解决这些问题提供了强大而灵活的工具。

探索C++编程的奥秘,分享深入的技术见解和实践,旨在激发读者创造力与解决问题的思维。 2299

C++序列化

借用c++元编程实现的序列化与反序列化,感谢大佬YaZi, B站Id: 天天写乱码, 提供的思路。

zdsji的博客 753

C++使用boost::serialization进行序列化

发现一个比较好玩的东西,boost::serialization序列化操作,简单来说感觉像ofstream和ifstream的升级版,Boost.Serialization 库能够将c++项目中的对象转换为一序列的比特(bytes),用来保存和加载还原对象。

超爱吃小蛋糕的66的博客 917

boost::serialization

boost::serialization 基于 boost::archive 来完成任意复杂数据结构的序列化,boost::archive提供两个实现类来完成序列化、反序列化操 作: boost::archive::text_oarchive     序列化数据,也称为:输出、保存(save) boost::archive::text_iarchive      反序列化数据

晴天的专栏 2017

boost/archive serialization 序列化 结构体字符串相互转换

Serialization的中文解释是“串行化” 、“序列化”或者“持久化” ,就是将内存中的对象保存到磁盘中,等到程序再次运行的时候再读取磁盘中的文件恢复原来的对象。 下面是一个序列化到字符串、字符串反序列化为结构体的例子 //st.h文件结构体定义 #include <string> #include <iostream> #include "boost/seri...

xuleisdjn的专栏 3523
上一篇: 西部数据绿盘、蓝盘、黑盘、红盘和紫盘有什么区别
下一篇: 北大清华2022年在各地录取人数排名
ctrigger
博客等级 码龄16年 1766粉丝 1577原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值