Use of Protobuf repeated type

Source: Internet
Author: User
Tags int size

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.