protobuf repeated的使用
来源:互联网 发布:软件项目介绍 编辑:程序博客网 时间:2024/05/09 10:09
protobuf是Google开发的一个序列化框架,类似XML,JSON,基于二进制,比传统的XML表示同样一段内容要短小得多。通过protobuf,可以很轻松的调用相关方法来完成业务数据的序列化与反序列化。protobuf repeated类型相当于std的vector,可以用来存放N个相同类型的内容,文章将简单介绍protobuf repeated的使用。
首先定义一个protobuf结构,如下:
message Person {
required int32 age = 1;
required string name = 2;
}
message Family {
repeated Person person = 1;
}
required int32 age = 1;
required string name = 2;
}
message Family {
repeated Person person = 1;
}
下面我们以例子简单说明如何使用:
int main(int argc, char* argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
Family family;
Person* person;
// 添加一个家庭成员,John
person = family.add_person();
person->set_age(25);
person->set_name("John");
// 添加一个家庭成员,Lucy
person = family.add_person();
person->set_age(23);
person->set_name("Lucy");
// 添加一个家庭成员,Tony
person = family.add_person();
person->set_age(2);
person->set_name("Tony");
// 显示所有家庭成员
int size = family.person_size();
cout << "这个家庭有 " << size << " 个成员,如下:" << endl;
for(int i=0; i<size; i++)
{
Person psn = family.person(i);
cout << i+1 << ". " << psn.name() << ", 年龄 " << psn.age() << endl;
}
getchar();
return 0;
}
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
Family family;
Person* person;
// 添加一个家庭成员,John
person = family.add_person();
person->set_age(25);
person->set_name("John");
// 添加一个家庭成员,Lucy
person = family.add_person();
person->set_age(23);
person->set_name("Lucy");
// 添加一个家庭成员,Tony
person = family.add_person();
person->set_age(2);
person->set_name("Tony");
// 显示所有家庭成员
int size = family.person_size();
cout << "这个家庭有 " << size << " 个成员,如下:" << endl;
for(int i=0; i<size; i++)
{
Person psn = family.person(i);
cout << i+1 << ". " << psn.name() << ", 年龄 " << psn.age() << endl;
}
getchar();
return 0;
}
0 0
- protobuf repeated的使用
- protobuf repeated类型的使用
- protobuf repeated类型的使用
- protobuf repeated类型的使用
- protobuf repeated类型的使用
- protobuf repeated类型的使用
- protobuf repeated类型的使用
- protobuf repeated类型的使用
- protobuf 使用 repeated 出错
- protobuf repeated的用法
- LuaFramework数据传输Protobuf的解析repeated字段
- protobuf的配置原始文件.proto中,使用修饰符repeated、required、optional生成的java文件的区别
- protobuf中 repeated[Ptr]Field的序列化
- protobuf的Required,Optional,Repeated限定修饰符
- protobuf的Required,Optional,Repeated限定修饰符
- protobuf协议的Required,Optional,Repeated限定修饰符
- protobuf的Required,Optional,Repeated限定修饰符
- google protobuf repeated 字段二义性
- SQL SERVER日常运维巡检系列
- Android获取当前系统时间
- binbinyang--5.MPAndroidChart开源图表库---柱状图
- Atlassian 信息化案例分享1
- 摘要XMPP初识
- protobuf repeated的使用
- input file里的JQ change() 事件的只生效一次
- Thrift源码系列----4.数据的解析与发送、接收
- 温故而知新-python语法复习
- Memcache
- npm、grunt、bower实现前端项目自动化
- viewpager 不带动画跳到指定页面
- redis和memcached的区别
- Vue.js学习笔记