protobuf repeated类型的使用
来源:互联网 发布:北京的猎头公司知乎 编辑:程序博客网 时间:2024/05/09 09:47
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类型数据结构
- protobuf repeated的用法
- LuaFramework数据传输Protobuf的解析repeated字段
- 给Protobuf中的repeated类型变量添加子项
- Protocol Buffers Lua API总结 -- 内置类型的repeated使用append() 复合类型的repeated使用add()
- protobuf的配置原始文件.proto中,使用修饰符repeated、required、optional生成的java文件的区别
- protobuf中 repeated[Ptr]Field的序列化
- protobuf的Required,Optional,Repeated限定修饰符
- protobuf的Required,Optional,Repeated限定修饰符
- C#笔试精简
- 11-《电子入门趣谈》第二章_电子电路的神经网络-2.1.2电容
- Live templates(模板)
- 离开一个人有多难
- hadoop2.6.0版本集群环境搭建
- protobuf repeated类型的使用
- python基础教程_list列表应用
- 一个小小的购物车系统
- 复选框CheckBox
- ExtJS5 - 认识MVVM
- js中的标签页title
- 2015-12-16
- Android 启动白屏问题解决
- google closure继承机制