当前位置:   article > 正文

2024年最新ProtoBuf3安装与使用_protobuf3 下载安装,2024年最新【C C++面试题】_protoc最新版本

protoc最新版本

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

syntax="proto3";
package tutorial;
message Person
{
	string name = 1;
	int32 id = 2;
	string email = 3;
 
	enum PhoneType
	{
		MOBILE = 0;
		HOME = 1;
		WORK = 2;
	}
 
	message PhoneNumber
	{
		string number = 1;
		PhoneType type = 2; 
	}
 
	repeated PhoneNumber phone = 4;
}
 
message AddressBook
{
	repeated Person person =1;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

可以看到该文件中,有一点类似C语言结构体的风格,自定义数据结构类型并且可以嵌套。Person类型中包含了三个基本类型:name,id,email,同时又包含一个复合类型PhoneNumber

编译该文件:

protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/Person.proto

//这里在当前路径下编译即可
protoc -I=./ --cpp_out=./ ./Person.pro

  • 1
  • 2
  • 3
  • 4
  • 5

编译后生成Person.proto,pb.hPerson.pro.pb.cc文件,这两个文件包含了Person以及AddressBook的类,以及对应操作类的方法,在你使用者这些类必须包含头文件,最终可执行文件也必须将cc文件也编译进去。

add_person.cpp 展示了如何设置一个联系人的各个字段的信息,并且最终序列化信息到文件中。

//add\_person.cpp
#include "Person.pro.pb.h"
#include <fstream>
#include <iostream>
using namespace std;
 
void PromptForAddress(tutorial::Person\*);
 
int main(int argc, char\* argv[])
{
	GOOGLE_PROTOBUF_VERIFY_VERSION;
	if(2 != argc)
	{
		//必须指定电话本名称才执行程序
	    cerr << "Usage: " << argv[0] << " ADDRESS\_BOOK\_FILE" << endl;
	    return -1;
	}
 
	tutorial::AddressBook address_book;
 
	fstream in("ADDRESS\_BOOK\_FILE", ios::binary | ios::in);
	if(!in)
	{
		cerr << "open file ADDRESS\_BOOK\_FILE failed!\n";
		return -1;
	}
	
	if(!address_book.ParseFromIstream(&in))
	{
		cerr << "Parse File ADDRESS\_BOOK\_FILE failed!\n";
		return -1;
	}
	
	in.close();
	//增加一个Person,可通过多次调用该接口增加联系人
	//具有repeated的属性可通过add\_fieldname方法增加一个属性
	PromptForAddress(address_book.add\_person());
 
	fstream out("ADDRESS\_BOOK\_FILE", ios::binary | ios::out | ios::trunc);
	if(!address_book.SerializeToOstream(&out))
	{
		cerr << "Failed to Write Address Book!\n";
		return -1;	
	}
 
	//可选的,回收所有ProtoBuf分配的对象
	google::protobuf::ShutdownProtobufLibrary();
	return 0;
}
 
 
void PromptForAddress(tutorial::Person\* person)
{
	cout<<"Enter a Person ID number: ";
	int id;
	cin >> id;
	person->set\_id(id);
	/\*忽略CIN的前256个字符,或者忽略CIN的换行符之前的字符,包括换行符
 这样的话不会将换行符之前的其他类型的数据保留在输入缓冲中 
 \*/
	cin.ignore(256, '\n');
	cout<<"Enter name: ";
	getline(cin, \*person->mutable\_name());
 
	cout<< "Enter email address (blank for none): ";
	string email;
	getline(cin,email);
	if(!email.empty())
		person->set\_email(email);
	while(true)
	{
		cout<<"Enter a phone number (or leave blank to finish): ";
		string number;
		getline(cin, number);
		if(number.empty())
			break;
		tutorial::Person::PhoneNumber\* phone_number = person->add\_phone();
		phone_number->set\_number(number);
 
		cout<<"Is this a mobile, home, or work phone? ";
		string type;
		getline(cin, type);
		if(type == "mobile")
			phone_number->set\_type(tutorial::Person::MOBILE);
		else if( type == "home")
			phone_number->set\_type(tutorial::Person::HOME);
		else if (type == "work")
			phone_number->set\_type(tutorial::Person::WORK);
		else
		{
			cout << "Unknown phone type. Using default." << endl;
			phone_number->set\_type(tutorial::Person::HOME);
		}
		
	}
	
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99

list_person.cpp展示了如何从文件中读取ProtoBuf序列化的信息

//list\_person.cpp
#include "Person.pro.pb.h"
#include <iostream>
#include <fstream>
 
using namespace std;
void ListPeople(const tutorial::AddressBook& address_book);
int main(int argc, char \* argv[])
{
	if(2!=argc)
	{
	    cerr << "Usage: " << argv[0] << " ADDRESS\_BOOK\_FILE" << endl;
	    return -1;
	}
 
	fstream in("ADDRESS\_BOOK\_FILE", ios::in | ios::binary);
	tutorial::AddressBook address_book;
	if(!address_book.ParseFromIstream(&in))
	{
	    cerr << "Parse Input File failed!"<< endl;
	    return -1;
	}
 
	ListPeople(address_book);
	google::protobuf::ShutdownProtobufLibrary();
	return 0;
}
 
void ListPeople(const tutorial::AddressBook& address_book)
{
	//fieldName\_size方法返回具有repeated属性的个数
	for(int i=0; i< address_book.person\_size();i++)
	{
		const tutorial::Person& person = address_book.person(i);
		cout<<"Person ID: "<<person.id();
		cout<<"Name: "<<person.name();
		cout<<"Email: "<<person.email();
		
		for(int j=0; j< person.phone\_size();j++)
		{
			const tutorial::Person::PhoneNumber& phone_number = person.phone(j);
			switch(phone_number.type())
			{
		        case tutorial::Person::MOBILE:
		          cout << " Mobile phone #: ";
		          break;
		        case tutorial::Person::HOME:
		          cout << " Home phone #: ";
		          break;
		        case tutorial::Person::WORK:
		          cout << " Work phone #: ";
		          break;
			}
			cout<<phone_number.number()<<endl;	
		}
		
	}
}




![img](https://img-blog.csdnimg.cn/img_convert/4fa82d24b65fb3db63d18052f4ffcb6e.png)
![img](https://img-blog.csdnimg.cn/img_convert/7b22806dba588d16606c99c50c7df1ef.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**

img-TEaW3e2x-1715661421624)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号