protobuf repeated use of array types

http://www.cppblog.com/API/archive/2014/12/09/209070.aspx

protobuf is a serialization framework developed by Google, is similar to XML, JSON, binary-based than traditional XML representation of the same piece of content to be shorter much smaller. By protobuf, you can easily call the relevant method to complete the serialization and de-serialization of business data. std corresponds to the type of protobuf repeated Vector, N can be used to store the same type of content, a quick overview article of protobuf repeated use.

Protobuf first define a structure as follows:

the Person {Message
  required Age = Int32. 1;
  required String name = 2;
}

Message {Family
  REPEATED the Person Person =. 1;
}
Here we illustrate a simple example of how to use:

int main(int argc, char* argv[])
{

    GOOGLE_PROTOBUF_VERIFY_VERSION;

    Family family;
    Person* person;

    // 添加一个家庭成员,John
    person = family.add_person();
    person->set_age(25);
    person->set_name("John");

    // 添加一个家庭成员,Lucy
    person = family.add_person();
    person->set_age(23);
    person->set_name("Lucy");

    // 添加一个家庭成员,Tony
    person = family.add_person();
    person->set_age(2);
    person->set_name("Tony");

    // 显示所有家庭成员
    int size = family.person_size();

    cout << "这个家庭有 " << size << " 个成员,如下:" << endl;

    for(int i=0; i<size; i++)
    {
        Person psn = family.person(i);
        cout << i+1 << ". " << psn.name() << ", 年龄 " << psn.age() << endl;
    }

    getchar();
    return 0;
}

 

Published 101 original articles · won praise 73 · views 120 000 +

Guess you like

Origin blog.csdn.net/usstmiracle/article/details/104635586