表类型

protobuf语言的maps字段类型相当于C++语言的std::map类型

工程目录结构

$ ls proto/
sample_maps.proto

proto文件

$ cat proto/sample_maps.proto
syntax = "proto3"; message home
{
string addr = 1;
} message sample_maps
{
map<int32, string> ids = 1;
map<string, home> infos = 2;
}

读写源文件

$ cat reader.cpp
#include <fstream>
#include <iostream>
#include "sample_maps.pb.h" using namespace std; int main(int argc, char *argv[])
{
fstream input("./log", ios::in | ios::binary);
cout << "Deserialize start." << endl; sample_maps m;
if (!m.ParseFromIstream(&input))
{
cout << "Deserialize failed." << endl;
return -1;
} auto ids = m.ids();
for (auto iter = ids.cbegin(); iter != ids.cend(); iter++)
cout << iter->first << " : " << iter->second << endl; auto infos = m.infos();
for (auto iter = infos.cbegin(); iter != infos.cend(); iter++)
cout << iter->first << " : " << iter->second.DebugString(); cout << "Deserialize end." << endl;
input.close();
return 0;
} $ cat writer.cpp
#include <fstream>
#include <iostream>
#include "sample_maps.pb.h" using namespace std; int main(int argc, char *argv[])
{
sample_maps m;
auto ids_ptr = m.mutable_ids();
(*ids_ptr)[1] = "fwd";
(*ids_ptr)[2] = "xb"; auto infos_ptr = m.mutable_infos();
home h;
h.set_addr("nj");
(*infos_ptr)["fwd"] = h; fstream output("./log", ios::out | ios::trunc | ios::binary);
cout << "Serialize start." << endl;
if (!m.SerializeToOstream(&output))
{
cout << "Serialize failed." << endl;
return -1;
}
output.close();
cout << "Serialize end." << endl;
return 0;
}

ProtoBuf练习(五)的更多相关文章

  1. erlang抽象码与basho的protobuf

    erlang抽象码与basho的protobuf(一)使用 erlang抽象码与basho的protobuf(二)代码生成原理之词法与语法分析 erlang抽象码与basho的protobuf(三)代 ...

  2. Mina、Netty、Twisted一起学(五):整合protobuf

    protobuf是谷歌的Protocol Buffers的简称,用于结构化数据和字节码之间互相转换(序列化.反序列化),一般应用于网络传输,可支持多种编程语言. protobuf如何使用这里不再介绍, ...

  3. Netty(五)序列化protobuf在netty中的使用

    protobuf是google序列化的工具,主要是把数据序列化成二进制的数据来传输用的.它主要优点如下: 1.性能好,效率高: 2.跨语言(java自带的序列化,不能跨语言) protobuf参考文档 ...

  4. protobuf中文教程(第一篇)

    声明:本文大部分内容翻译自官方英文文档,其中可能穿插着加入自己的语言用以辅助理解,本文禁止转载. 一.什么是protocol buffers Protocol buffers是一个灵活的.高效的.自动 ...

  5. 解决protobuf不能直接在IOS上使用,利用protobuf-net在IOS上通讯

    ---------------------------------------------------------------------------------------------------- ...

  6. 在ios android设备上使用 Protobuf (使用dll方式)

    http://game.ceeger.com/forum/read.php?tid=13479 如果你的工程可以以.Net 2.0 subset模式运行,请看这个帖子中的方法. 地址:http://g ...

  7. (一)Protobuf的Java使用

    学习使用Protobuf,创建java文件 windows : 步骤一:两个文件:proto.exe,  protobuf-Java-2.4.1.jar 步骤二:建立一个工程CreateProtoBu ...

  8. Netty4.x中文教程系列(五)编解码器Codec

    Netty4.x中文教程系列(五)编解码器Codec 上一篇文章详细解释了ChannelHandler的相关构架设计,版本和设计逻辑变更等等. 这篇文章主要在于讲述Handler里面的Codec,也就 ...

  9. Mac IDEA插件——protobuf 插件

    最近在搞PB的版本的升级,历史的PB的版本是2.4,现在是打算升级到3.2,当面PB的版本肯定有很多变化了,就不再这里多说了,这里重点说一说,采用IDEA的插件方便执行PB的文件的JAVA编译,这样的 ...

随机推荐

  1. SetOperations

    无序集合,add的顺序不是存储顺序 1.add(K key, V value) 2.difference(K key, otherK[s]) :差集,返回Set 3.differenceAndStor ...

  2. Django-01

    知识预览 Django基本命令 二 路由配置系统(URLconf) 三 编写视图 四 Template 五 数据库与ORM admin的配置 一 什么是web框架? 框架,即framework,特指为 ...

  3. C. Jury Marks

    C. Jury Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. 判断CPU是大端还是小端

    #include "stdafx.h" #include <iostream> using namespace std; /* #大端模式(Big_endian):字数 ...

  5. 每天一个linux命令(6):rm命令

    版权声明更新:2017-05-10博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1文章介绍 本文介绍了Linux下面的rm命令. 2 开 ...

  6. Windows下安装MySQL-5.7.19

    下载MySQL-5.7.19版本,解压之后发现安装包不是一个exe文件: 于是网上找资料,发现了另外一种安装方法. 1.新建一个环境变量,我的电脑-属性-高级-环境变量-系统变量-新建 2.在系统变量 ...

  7. Poj 1316 Self Numbers(水题)

    一.Description In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called se ...

  8. Poj1298_The Hardest Problem Ever(水题)

    一.Description Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar eve ...

  9. 上海-北京间通过Azure Storage的RA-GRS类型的存储账户进行快速复制

    Azure的Blob存储分成多种类型,目前主要有: 其中RA-GRS可以在上海-北京两个数据中心间同步数据.并且,在第二个数据中心可以只读的方式读取这个存储账户中的Blob内容. 虽然GRS采用的是准 ...

  10. Cypress USB3014 C++DLL 导入问题

    VS2017编译cpp工程出现问题 硬件型号:芯片版本 Cypress FX3 USB3014(和芯片无关)) 重现步骤: 1.解压 FX3_SDK_Windows_v1.3.3.exe 2.VS20 ...