c++中编码protobuf repeated string
参考:http://www.cppblog.com/API/archive/2014/12/09/209070.aspx
proto文件
addressbook.proto
syntax = "proto3";
package lm;
message group_s
{
string group_name = 1;
repeated string member_name = 2;
}
C++程序
- 编码proto文件
probufwriter.cpp
#include "addressbook.pb.h"
#include <fstream>
#include <iostream>
using namespace std;
int main(void)
{
lm::group_s msg1;
msg1.set_group_name("Class-1");
for(int j =0; j<3; j++){
std::string m_name = "student_"+to_string(j);
msg1.add_member_name(m_name);
cout << msg1.member_name(j) << endl; //打印出string数组元素
}
// Write the new address book back to disk.
fstream output("./log", ios::out | ios::trunc | ios::binary);
if (!msg1.SerializeToOstream(&output)) {
cerr << "Failed to write msg." << endl;
return -1;
}
return 0;
}
- 解码proto文件
probufread.cpp
#include "addressbook.pb.h"
#include <iostream>
#include <fstream>
using namespace std;
void ListMsg(const lm::group_s & msg) {
for(int j =0; j<3; j++){
cout << msg.member_name(j) << endl;
}
cout << msg.group_name() << endl;
}
int main(int argc, char* argv[])
{
lm::group_s msg1;
{
fstream input("./log", ios::in | ios::binary);
if (!msg1.ParseFromIstream(&input)) {
cerr << "Failed to parse address book." << endl;
return -1;
}
}
ListMsg(msg1);
}
编译运行
编译环境
安装好protobuf库
安装方法:https://www.cnblogs.com/abc36725612/p/14196674.html
编译
参考:https://www.cnblogs.com/huanyinglvtuan/p/14119328.html
- 生成c++结构体
# protoc -I=. --cpp_out=. ./addressbook.proto
# ls
README.md addressbook.pb.cc addressbook.pb.h addressbook.proto probufread.cpp probufwriter.cpp
编译c++程序
# g++ -std=c++11 -g -Wall addressbook.pb.cc probufwriter.cpp -o s -lprotobuf -I /include -lpthread
# g++ -std=c++11 -g -Wall addressbook.pb.cc probufread.cpp -o c -lprotobuf -I /include -lpthread
运行
# ./s
student_0
student_1
student_2
# ./c
student_0
student_1
student_2
Class-1
c++中编码protobuf repeated string的更多相关文章
- lua 中protobuf repeated 嵌套类 复合类型
PB基础知识科普 syntax = "proto2"; package PB; message Item { required string name = ; } message ...
- 在egret中使用protobuf
原文章删除,重新使用MarkDown排版 在H5游戏领域,对于服务端与客户端的通信协议有一个选择,那就是使用protobuf.js.对于那些直接使用JavaScript开发的引擎而言,protobuf ...
- 在 Java 中使用 protobuf
在 Java 中使用 protobuf 从 https://github.com/google/protobuf/releases 下载编译器,并设置环境变量. 创建java项目添加protobuf- ...
- 在erlang项目中使用protobuf
在erlang项目中使用protobuf http://blog.csdn.net/mycwq/article/details/21864191 protobuf是google的一个序列化框架,类似X ...
- netty系列之:在netty中使用protobuf协议
目录 简介 定义protobuf 定义handler 设置ChannelPipeline 构建client和server端并运行 总结 简介 netty中有很多适配不同协议的编码工具,对于流行的goo ...
- 在Wcf中应用ProtoBuf替代默认的序列化器
Google的ProtoBuf序列化器性能的牛逼已经有目共睹了,可以把它应用到Socket通讯,队列,Wcf中,身为dotnet程序员一边期待着不久后Grpc对dotnet core的支持更期待着Wc ...
- webapi 中使用 protobuf
相比json来说,好处是速度更快,带宽占用更小.其效果大致等于json+Gzip. 在webapi中使用protobuf的方法为: 引用nuget包 Install-Package protobuf- ...
- 在网络通讯中应用Protobuf
在网络通讯中应用Protobuf Protobuf的设计非常适用于在网络通讯中的数据载体,它序列化出来的数据量少再加上以K-V的方式来存储数据,对消息的版本兼容性非常强:还有一个比较大的优点就是有着很 ...
- 怎样在Spark、Flink应用中使用Protobuf 3的包
如果在在Spark.Flink应用中使用Protobuf 3的包,因为Spark默认使用的是2.5版本的包,提交任务时,可能会报如下异常: com.google.protobuf.CodedInput ...
随机推荐
- js数组去重,id相同对某值相加合并
js数组去重,id相同对某值相加合并 案例1: 假设需要处理的数组结构. let arr =[ {id:'1', value:10}, {id:'1', value:20}, {id:'2', val ...
- 题解 CF327A Flipping Game
前言 数据水的一批,\(\mathcal{O}(n^3)\) 给过我觉得是不应该的. 题意 有一个由 \(0\) 和 \(1\) 组成的序列 \(a_1,a_2,a_3,a_4....,a_n\) . ...
- curl 下载地址中有特殊字符解决方案
curl 下载地址中有特殊字符解决方案 情况 使用 curl 下载 地址中带有 特殊字符的时候 比如下面这个地址.实际访问地址不正确,参数丢失问题 curl -o kspf.jpeg https:// ...
- Golang Gorm time 时间字段格式化模型类 重写
问题: 在使用GORM中 如果我们使用到了CreateAt 和UpdateAt 就会发现 这个时间的类型是time.Time 而其数据是 "2022-10-13T10:14:02.97352 ...
- .NET 7 中 LINQ 的疯狂性能提升
LINQ 是 Language INtegrated Query 单词的首字母缩写,翻译过来是语言集成查询.它为查询跨各种数据源和格式的数据提供了一致的模型,所以叫集成查询.由于这种查询并没有制造新的 ...
- Django系列---开发二
django.contrib.auth Django的用户验证框架,可以快速实现用户信息验证.登录.登出等用户操作 from django.contrib.auth import authentica ...
- C#怎么在生成解决方案的过程中执行perl脚本(C#早期绑定)
转载 怎么在生成解决方案的过程中执行perl脚本 早期绑定在编译期间识别并检查方法.属性.函数,并在应用程序执行之前执行其他优化.在这个绑定中,编译器已经知道它是什么类型的对象以及它拥有的方法或属性. ...
- 模块/collections/random/time/datetime
内容概要 模块--包的具体使用 编程思想介绍 软件开发--目录规范 常用模块介绍--collections模块 常用模块介绍--time.datetime 常用模块介绍--random 1.包的具体使 ...
- java:绘制图形
java绘图类:Graphics类 绘图是高级程序中必备的技术,在很多方面都能用到,如:绘制闪屏图片,背景图片和组件外观等. 1.Graphics类 Graphics类是所有图形上下文的抽象基类,Gr ...
- VsCode搭建一个React项目
安装Node.js 使用 npm -v检查安装成功 目前的 node 中都会自带 npm 所以不需要重新下载 直接切换至淘宝镜像即可 1.临时使用 :npm --registry https://re ...