ProtoBuf练习(五)
表类型
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练习(五)的更多相关文章
- erlang抽象码与basho的protobuf
erlang抽象码与basho的protobuf(一)使用 erlang抽象码与basho的protobuf(二)代码生成原理之词法与语法分析 erlang抽象码与basho的protobuf(三)代 ...
- Mina、Netty、Twisted一起学(五):整合protobuf
protobuf是谷歌的Protocol Buffers的简称,用于结构化数据和字节码之间互相转换(序列化.反序列化),一般应用于网络传输,可支持多种编程语言. protobuf如何使用这里不再介绍, ...
- Netty(五)序列化protobuf在netty中的使用
protobuf是google序列化的工具,主要是把数据序列化成二进制的数据来传输用的.它主要优点如下: 1.性能好,效率高: 2.跨语言(java自带的序列化,不能跨语言) protobuf参考文档 ...
- protobuf中文教程(第一篇)
声明:本文大部分内容翻译自官方英文文档,其中可能穿插着加入自己的语言用以辅助理解,本文禁止转载. 一.什么是protocol buffers Protocol buffers是一个灵活的.高效的.自动 ...
- 解决protobuf不能直接在IOS上使用,利用protobuf-net在IOS上通讯
---------------------------------------------------------------------------------------------------- ...
- 在ios android设备上使用 Protobuf (使用dll方式)
http://game.ceeger.com/forum/read.php?tid=13479 如果你的工程可以以.Net 2.0 subset模式运行,请看这个帖子中的方法. 地址:http://g ...
- (一)Protobuf的Java使用
学习使用Protobuf,创建java文件 windows : 步骤一:两个文件:proto.exe, protobuf-Java-2.4.1.jar 步骤二:建立一个工程CreateProtoBu ...
- Netty4.x中文教程系列(五)编解码器Codec
Netty4.x中文教程系列(五)编解码器Codec 上一篇文章详细解释了ChannelHandler的相关构架设计,版本和设计逻辑变更等等. 这篇文章主要在于讲述Handler里面的Codec,也就 ...
- Mac IDEA插件——protobuf 插件
最近在搞PB的版本的升级,历史的PB的版本是2.4,现在是打算升级到3.2,当面PB的版本肯定有很多变化了,就不再这里多说了,这里重点说一说,采用IDEA的插件方便执行PB的文件的JAVA编译,这样的 ...
随机推荐
- codevs1279 Guard 的无聊
题目描述 Description 在那楼梯那边数实里面,有一只 guard,他活泼又聪明,他卖萌又霸气.他每天刷题虐 场 D 人考上了 PKU,如果无聊就去数一数质数~~ 有一天 guard 在纸上写 ...
- Nexus4_识别电池真假
1.参考网址:http://bbs.gfan.com/android-7509786-1-1.html ([6_25更新]给N4换一个“原装的芯”&[免拆]识别真假电池 - LG Nexus ...
- WebForm 使用点滴。。。。
WebForm使用方式与WinForm很是相似,可控性非常高! 1.调用别的按钮事件: BtnSelect_Click(sender, e);
- BEC listen and translation exercise 39
What about jigsaw puzzle design for visually handicapped?给视觉障碍人士设计拼图怎么样? Length is 50cm, and then th ...
- ADO:游标与RecordSetPtr
一.游标的属性设置: 游标是记录集recordset中用于游动逐个查找一条记录的一种机制.查询的数据返回后,通过游标来逐条获取查询的记录. 因此在配置数据库查询方式前可配置游标的属性: rs.Curs ...
- freeMarker(九)——程序开发指南补充知识
学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 1.变量.范围 本章介绍当模板在访问变量时发生了什么事情,还有变量是如 ...
- bzoj 4540: [Hnoi2016]序列 莫队
题目: 给定长度为n的序列:a1,a2,-,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是指序列:al,al+1,-,ar- 1,ar.若1≤l≤s≤t≤r≤n,则称a[s:t]是a ...
- Oracle 12C 新特性之级联truncate
12c之前的版本中,在子表引用一个主表以及子表存在记录的情况下,是不提供截断此主表操作的.而在 12c 中的带有 CASCADE 操作的TRUNCATE TABLE 可以截断主表中的记录,并自动对子表 ...
- H5 限制input只能输入数字
<input type="tel" /> 参考: http://blog.csdn.net/kongjiea/article/details/40185951
- bzoj 1711 Dining吃饭 —— 最大流
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1711 食物一列,牛拆点,饮料一列. 代码如下: #include<cstdio> ...