日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Protocol Buffer】Protocol Buffer入门教程(五):repeated限定修饰符

發布時間:2024/4/24 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Protocol Buffer】Protocol Buffer入门教程(五):repeated限定修饰符 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

00. 目錄

文章目錄

    • 00. 目錄
    • 01. 限定修飾符介紹
    • 02. 字段API分析
    • 03. 測試代碼
    • 04. 編譯和測試
    • 05. 參考

01. 限定修飾符介紹

repeated 代表可重復,我們可以理解為數組

syntax = "proto3";//指定版本信息,不指定會報錯message Person //message為關鍵字,作用為定義一種消息類型 {string name = 1; //姓名int32 id = 2; //idstring email = 3; //郵件 }message AddressBook {repeated Person people = 1; }

02. 字段API分析

deng@itcast:/mnt/hgfs/LinuxHome/day03$ protoc addressbook.proto --cpp_out=./ deng@itcast:/mnt/hgfs/LinuxHome/day03$

而對于字段修飾符為repeated的字段生成的函數,則稍微有一些不同,如people字段,則編譯器會為其產生如下的代碼:

enum : int {kPeopleFieldNumber = 1,}; // repeated .Person people = 1;int people_size() const;private:int _internal_people_size() const;public:void clear_people();::Person* mutable_people(int index);::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::Person >*mutable_people();private:const ::Person& _internal_people(int index) const;::Person* _internal_add_people();public:const ::Person& people(int index) const;::Person* add_people();const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::Person >&people() const;

03. 測試代碼

參考代碼如下:

#include "addressbook.pb.h" #include <iostream> #include <fstream>using namespace std;void set_addressbook() {AddressBook obj;Person *p1 = obj.add_people(); //新增加一個Personp1->set_name("tom");p1->set_id(1);p1->set_email("tom@qq.com");Person *p2 = obj.add_people(); //新增加一個Personp2->set_name("jim");p2->set_id(2);p2->set_email("jim@qq.com");Person *p3 = obj.add_people(); //新增加一個Personp3->set_name("abc");p3->set_id(3);p3->set_email("abc@qq.com");fstream output("pb.itcast", ios::out | ios::trunc | ios::binary);bool flag = obj.SerializeToOstream(&output);//序列化if (!flag){cerr << "Failed to write file." << endl;return;}output.close();//關閉文件 }void get_addressbook() {AddressBook obj;fstream input("./pb.itcast", ios::in | ios::binary);obj.ParseFromIstream(&input); //反序列化input.close(); //關閉文件for (int i = 0; i < obj.people_size(); i++){const Person& person = obj.people(i);//取第i個peoplecout << "第" << i + 1 << "個信息\n";cout << "name = " << person.name() << endl;cout << "id = " << person.id() << endl;cout << "email = " << person.email() << endl << endl;} }int main() {// Verify that the version of the library that we linked against is// compatible with the version of the headers we compiled against.GOOGLE_PROTOBUF_VERIFY_VERSION;set_addressbook(); //序列化get_addressbook(); //反序列化// Optional: Delete all global objects allocated by libprotobuf.google::protobuf::ShutdownProtobufLibrary();return 0; }

04. 編譯和測試

編譯和測試

deng@itcast:/mnt/hgfs/LinuxHome/day03$ g++ test.cpp addressbook.pb.cc `pkg-config --libs --cflags protobuf` deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out 第1個信息 name = tom id = 1 email = tom@qq.com第2個信息 name = jim id = 2 email = jim@qq.com第3個信息 name = abc id = 3 email = abc@qq.comdeng@itcast:/mnt/hgfs/LinuxHome/day03$

05. 參考

官方參考文檔:https://developers.google.cn/protocol-buffers/docs/reference/cpp-generated#repeatednumeric

測試代碼下載:測試代碼下載

總結

以上是生活随笔為你收集整理的【Protocol Buffer】Protocol Buffer入门教程(五):repeated限定修饰符的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。