boost 序列化1

 http://dozb.bokee.com/1692310.html#simplecase

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
using namespace boost::archive;
#include <sstream>


int main()
{

    int a = 10;                // 4
    string s("你好!");        // 序列化时字节是9
    cout << s.size() << endl;// 5字节长度(2字节/汉字 + 4字节(成员大小))
    uint32_t b = 23;        // 4
    string ss("abcdfrg");    // 序列化时字节是11(7字母 + 4字节(成员大小))
    cout << ss.size() << endl;
    int64_t c = 2456;        // 8
    // char、double会有问题
    //char d = 'a';
    //double e = 3.14;

    std::stringstream os;
    boost::archive::binary_oarchive arSend(os, boost::archive::no_header);
    arSend << a << s << b << ss << c; //<< d << e;
    cout << os.str().size() << endl;// 36


    int a1 = 0;
    string s1("");
    uint32_t b1 = 0;
    string ss1("");
    int32_t c1 = 0;
    //char d1 = 'h';
    //double e1 = 0.0;
    boost::archive::binary_iarchive arRecv(os, boost::archive::no_header);
    arRecv >> a1;
    cout << "a1:    " << a1 << endl;//10
    arRecv >> s1;
    cout << "s1:    " << s1 << endl;//你好!
    arRecv >> b1;
    cout << "b1:    " << b1 << endl;//23
    arRecv >> ss1;
    cout << "ss1:    " << ss1 << endl;//abcdfrg
    arRecv >> c1;
    cout << "c1:    " << c1 << endl;//2456
    //arRecv >> d1;
    //cout << "d1:    " << d1 << endl;
    //arRecv >> e1;
    //cout << "e1:    " << e1 << endl;

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值