程序性能优化之网络传输与数据存储优化(五)上

int main()
{
int fd = open(“./testFileDesc.xxx”, O_CREAT|O_TRUNC|O_RDWR, 0664);

GOOGLE_PROTOBUF_VERIFY_VERSION;

Person obj;
obj.set_name(“gongluck”);
obj.set_id(1);
*obj.mutable_email() = “http://blog.csdn.net/gongluck93”;
obj.SerializeToFileDescriptor(fd);
fsync(fd);
lseek(fd, 0, SEEK_SET);

Person obj2;
obj2.ParseFromFileDescriptor(fd);
cout << "name = " << obj2.name() << endl;
cout << "id = " << obj2.id() << endl;
cout << "email = " << obj2.email() << endl;

google::protobuf::ShutdownProtobufLibrary();

return 0;
}

C++ stream 序列化和反序列化

#include
#include
#include “person.pb.h”

using namespace std;

int main()
{
fstream file(“testStream.xxx”, ios::in|ios::out|ios::trunc|ios::binary);

GOOGLE_PROTOBUF_VERIFY_VERSION;

Person obj;
obj.set_name(“gongluck”);
obj.set_id(1);
*obj.mutable_email() = “http://blog.csdn.net/gongluck93”;
obj.SerializeToOstream(&file);
file.flush();
file.seekg(0, ios::beg);

Person obj2;
obj2.ParseFromIstream(&file);
cout << "name = " << obj2.name() << endl;
cout << "id = " << obj2.id() << endl;
cout << "email = " << obj2.email() << endl;

google::protobuf::ShutdownProtobufLibrary();

file.close();

return 0;
}

repeated限定修饰符

repeated 代表可重复,我们可以理解为数组。

syntax = “proto3”;

message Person
{
string name = 1;
int32 id = 2;
string email = 3;
}

message AddressBook
{
repeated Person people = 1;
}

而对于字段修饰符为repeated的字段生成的函数,则稍微有一些不同,如people字段,则编译器会为其产生如下的代码:

int people_size() cons

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值