Link: http://www.cppblog.com/API/archive/2014/12/09/209070.aspx
PROTOBUF is a serialized framework of Google Development, similar to Xml,json, based on binary, which is much shorter than the traditional XML representation of the same section. Through the PROTOBUF, it is easy to call the relevant methods to complete the serialization and deserialization of business data. The Protobuf repeated type is equivalent to the vector of STD, which can be used to store n the same type of content, the article will briefly introduce the use of protobuf repeated.
First, define a PROTOBUF structure, as follows:
Message person {
Required Int32 age = 1;
Required String name = 2;
}
Message Family {
Repeated person person = 1;
}
Here's a quick example of how to use:
int main (int argc, char* argv[])
{
Google_protobuf_verify_version;
Family Family;
person* person;
Add a family member, John
person = Family.add_person ();
Person->set_age (25);
Person->set_name ("John");
Add a family member, Lucy
person = Family.add_person ();
Person->set_age (23);
Person->set_name ("Lucy");
Add a family member, Tony.
person = Family.add_person ();
Person->set_age (2);
Person->set_name ("Tony");
Show all family members
int size = Family.person_size ();
cout << "This family has" << size << "members, as follows:" << Endl;
for (int i=0; i<size; i++)
{
Person PSN = Family.person (i);
cout << i+1 << "." << psn.name () << ", Age" << psn.age () << Endl;
}
GetChar ();
return 0;
}