问题描述:

Can somebody give me an example of packing\unpacking message with repeating sub-message in protobuf-c?

Official documentation in empty on this matter.

网友答案:
message B {
  required int64 i = 1;
};

message A {
  repeated B numbers = 1;
};

A a;
a.ParseFromString(<str>)
for (int i = 0; i < a.numbers_size(); ++i) {
  const B& b = a.numbers(i);
  printf("%d\n", b.i());
}
网友答案:

"Reading A Message" section of Google's C++ tutorial has relevant sample code: http://code.google.com/apis/protocolbuffers/docs/cpptutorial.html

Also, STL's for_each also appears to work OK (syntactic sugar, more-or-less): std::for_each(a.numbers().begin() ,a.numbers().end() ,[](const B& b) { std::cout << b.i() << std::endl ; } ) ;

相关阅读:
Top