表类型

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. pyqt5开发之俄罗斯方块

    #!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This is a Tetri ...

  2. 【海量之道】海量之道之SET模型

    本文介绍了set模型. 一 提供海量服务时面对的场景 场景1:如何令黄村机房的TWS机器访问黄村机房的APP服务,避免TWS跨机房调用永丰机房的APP机器? 场景2:DB和Redis如何实现快慢分离, ...

  3. Java之泛型深解

    泛型的内容确实很多,在上一篇Java之泛型浅解讲到了一些常用的泛型,但是还远远不够,上一篇的内容比较容易理解,这一篇我自己觉得更加难理解一些,因此,我还得想办法让它更加接地气更加容易理解,方便我和源宝 ...

  4. Java微信开发_Exception_01_The type org.xmlpull.v1.XmlPullParser cannot be resolved. It is indirectly referenced from required .class files

    一.源码: package com.souvc.weixin.util; import java.io.InputStream; import java.io.Writer; import java. ...

  5. listen and translation exercise 49

    Huh? Appears to Be Universally Understood What's the most universal utterance in languages across th ...

  6. hihocoder-1285 智力竞赛(区间dp)

    智力竞赛 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi.小Ho还有被小Hi强拉来的小Z,准备组队参加一个智力竞赛.竞赛采用过关制,共计N个关卡.在第i个关卡中,小 ...

  7. Maven 排除依赖jar包

    当我们引入第三方jar包的时候,难免会引入传递性依赖,有些时候这是好事,然而有些时候我们不需要其中的一些传递性依赖 比如我们不想引入传递性依赖commons-logging,我们可以使用exclusi ...

  8. 拓扑排序 POJ 1094 Sorting It All Out

    题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...

  9. Linux CentOS安装Azure Cli工具

    vim /etc/yum.repos.d/epel.repo [epel] name=epel baseurl=http://mirrors.sohu.com/fedora-epel/6/$basea ...

  10. TModJS:README

    ylbtech-TModJS:README 1.返回顶部 1. TmodJS 项目已经停止维护,请使用更好的代替方案:art-template-loader TmodJS(原名 atc)是一个简单易用 ...