zoukankan      html  css  js  c++  java
  • 测试了下boost的序列化反序列化功能

    [cpp] view plain copy
     
    1. // testSerialization.cpp : Defines the entry point for the console application.  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5.   
    6. #include <boost/archive/text_oarchive.hpp>  
    7. #include <boost/archive/text_iarchive.hpp>  
    8. #include <boost/serialization/vector.hpp>  
    9.   
    10. #include <string>  
    11. #include <vector>  
    12. #include <iostream>  
    13. #include <sstream>    
    14. #include <fstream>  
    15.   
    16. /* 
    17. 标题:boost的序列化反序列化功能测试 
    18. 环境:VS2010sp1、boost 1.55 
    19. 参考资料: 
    20. [1]《Boost中支持序列化反序列化的库--boost.serialization 》 
    21. http://blog.csdn.net/oracleot/article/details/4280084 
    22. [2]《What does BOOST_SERIALIZATION_NVP do when serializing object?》 
    23. http://stackoverflow.com/questions/8532331/what-does-boost-serialization-nvp-do-when-serializing-object 
    24. */  
    25.   
    26. struct MyRecord  
    27. {  
    28.     std::string a;  
    29.     std::string b;  
    30.     //为使当前的结构能支持序列化,得加入下面的代码段  
    31. private:  
    32.     friend class boost::serialization::access;  
    33.     template<class Archive>  
    34.     void serialize(Archive & ar, const unsigned int version)  
    35.     {  
    36.         ar & a;  
    37.         ar & b;  
    38.     }  
    39. };  
    40.   
    41. struct MyData{  
    42.     int id;  
    43.     std::string strName;  
    44.     std::string strValue;  
    45.   
    46.     MyData() {}  
    47.     MyData(int id, std::string strName, std::string strValue)  
    48.     {  
    49.         this->id = id;  
    50.         this->strName = strName;  
    51.         this->strValue = strValue;  
    52.     }  
    53.   
    54.     std::vector<MyRecord> listMR;  
    55.   
    56. private:  
    57.     friend boost::serialization::access;          //声明友元,授予访问权限  
    58.     template<typename Archive>  
    59.     void serialize(Archive &ar,const unsigned int version)     //序列化函数  
    60.     {  
    61.         ar & id;  
    62.         ar & strName & strValue;  
    63.         ar & listMR;  
    64.     }  
    65. };  
    66.   
    67. void printTest(MyData &mt)  
    68. {  
    69.     std::cout<<"a="<<mt.id<<" uid="<<mt.strName.c_str()<<" usr="<<mt.strValue.c_str()<<std::endl;  
    70.   
    71.     std::vector<MyRecord>::iterator iter = mt.listMR.begin();  
    72.     while(iter!=mt.listMR.end())  
    73.     {  
    74.         std::cout<<"=> "<<iter->a.c_str()<<"  "<<iter->b.c_str()<<std::endl;  
    75.         iter++;  
    76.     }  
    77. }  
    78.   
    79. int _tmain(int argc, _TCHAR* argv[])  
    80. {  
    81.     std::stringstream ss;  
    82.   
    83.     //序列化  
    84.     MyData p1(11,"anderson","neo");      //被序列化的对象  
    85.     //为被序列化对象添加两条记录  
    86.     MyRecord mr,mr2;  
    87.     mr.a="apple",mr.b="第一条记录";  
    88.     mr2.a = "this is b",mr2.b = "第二条记录";  
    89.     p1.listMR.push_back(mr);  
    90.     p1.listMR.push_back(mr2);  
    91.   
    92.     boost::archive::text_oarchive(ss) << p1; //序列化  
    93.     //序列化为xml形式要求中文为utf-8编码,否则打开文件是乱码  
    94.     //std::stringstream ss;  
    95.     //std::ofstream ofs("d:\a.xml");  
    96.     //boost::archive::xml_oarchive oa(ofs);  
    97.     //oa << BOOST_SERIALIZATION_NVP(p1);  
    98.   
    99.   
    100.     //反序列化  
    101.     MyData p2;                               //被反序列化的对象  
    102.     boost::archive::text_iarchive(ss) >> p2; //反序列化  
    103.   
    104.     printTest(p2);  
    105.     std::cout << "序列化后的=>" <<    ss.str() << std::endl;;  
    106.   
    107.     return 0;  
    108. }  

    http://blog.csdn.net/lee353086/article/details/38421095

  • 相关阅读:
    奇数阶魔方问题
    《DSP using MATLAB》示例9.3
    《DSP using MATLAB》示例9.2
    《DSP using MATLAB》示例9.1
    找个目标很重要
    《DSP using MATLAB》示例Example 8.30
    《DSP using MATLAB》示例Example 8.29
    《DSP using MATLAB》示例Example 8.28
    《DSP using MATLAB》示例Example 8.27
    《DSP using MATLAB》示例Example 8.26
  • 原文地址:https://www.cnblogs.com/findumars/p/7635864.html
Copyright © 2011-2022 走看看