参考: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的更多相关文章

  1. lua 中protobuf repeated 嵌套类 复合类型

    PB基础知识科普 syntax = "proto2"; package PB; message Item { required string name = ; } message ...

  2. 在egret中使用protobuf

    原文章删除,重新使用MarkDown排版 在H5游戏领域,对于服务端与客户端的通信协议有一个选择,那就是使用protobuf.js.对于那些直接使用JavaScript开发的引擎而言,protobuf ...

  3. 在 Java 中使用 protobuf

    在 Java 中使用 protobuf 从 https://github.com/google/protobuf/releases 下载编译器,并设置环境变量. 创建java项目添加protobuf- ...

  4. 在erlang项目中使用protobuf

    在erlang项目中使用protobuf http://blog.csdn.net/mycwq/article/details/21864191 protobuf是google的一个序列化框架,类似X ...

  5. netty系列之:在netty中使用protobuf协议

    目录 简介 定义protobuf 定义handler 设置ChannelPipeline 构建client和server端并运行 总结 简介 netty中有很多适配不同协议的编码工具,对于流行的goo ...

  6. 在Wcf中应用ProtoBuf替代默认的序列化器

    Google的ProtoBuf序列化器性能的牛逼已经有目共睹了,可以把它应用到Socket通讯,队列,Wcf中,身为dotnet程序员一边期待着不久后Grpc对dotnet core的支持更期待着Wc ...

  7. webapi 中使用 protobuf

    相比json来说,好处是速度更快,带宽占用更小.其效果大致等于json+Gzip. 在webapi中使用protobuf的方法为: 引用nuget包 Install-Package protobuf- ...

  8. 在网络通讯中应用Protobuf

    在网络通讯中应用Protobuf Protobuf的设计非常适用于在网络通讯中的数据载体,它序列化出来的数据量少再加上以K-V的方式来存储数据,对消息的版本兼容性非常强:还有一个比较大的优点就是有着很 ...

  9. 怎样在Spark、Flink应用中使用Protobuf 3的包

    如果在在Spark.Flink应用中使用Protobuf 3的包,因为Spark默认使用的是2.5版本的包,提交任务时,可能会报如下异常: com.google.protobuf.CodedInput ...

随机推荐

  1. PHP全栈开发(八):CSS Ⅵ 列表 style

    列表分为有序列表和无序列表 我们知道有序列表的标签是<ol>意思是order list 无序列表的标签是<ul> 列表里面每项的标签用<li>来进行包裹. 使用CS ...

  2. POJ1734 Sightseeing trip (Floyd求最小环)

    学习了一下用Floyd求最小环,思路还是比较清晰的. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring ...

  3. Hbase之权限控制

    Hbase之权限控制 -- 只读权限 grant '{userName}','R','{namespaceName:tableName}' -- 写入权限 grant '{userName}','W' ...

  4. .NET周报【10月第2期 2022-10-17】

    主题 宣布 .NET 7 发布候选版本 2 - .NET Blog https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-rc-2/ .N ...

  5. Linux系统管理_用户管理

    cat /etc/passwd #账户文件 cat /etc/shadow #密码文件 cat /etc/login.defs #密码策略机UID定义文件 #普通用户UID范围1000~60000:系 ...

  6. 齐博x2是什么?

    齐博x2是什么? 齐博x2是齐博x1的延申版本. 对小程序全方位加强

  7. Netty学习记录-入门篇

    你如果,缓缓把手举起来,举到顶,再突然张开五指,那恭喜你,你刚刚给自己放了个烟花. 模块介绍 netty-bio: 阻塞型网络通信demo. netty-nio: 引入channel(通道).buff ...

  8. 搜索"xxxx"的进程,同时杀进程

    一.搜索"xxxx"的进程,同时杀进程,命令如下: ps -ef|grep xxxx|grep -v 'grep'|awk '{print $2}'|xargs kill -9 命 ...

  9. 10 | Kubernetes一键部署利器:kubeadm

    你好,我是张磊.今天我和你分享的主题是:Kubernetes一键部署利器之kubeadm. 通过前面几篇文章的内容,我其实阐述了这样一个思想:要真正发挥容器技术的实力,你就不能仅仅局限于对Linux容 ...

  10. Go语言核心36讲24

    你好,我是郝林,今天我们继续来聊聊panic函数.recover函数以及defer语句的内容. 我在前一篇文章提到过这样一个说法,panic之中可以包含一个值,用于简要解释引发此panic的原因. 如 ...