How to extract map field from protobuf message for C++ consumer

709 Views Asked by At

I have a proto3 file. There is a message in the proto file that contains a map field.

proto3
package abc.xyz.testproto
message abc
{
    map <string, int32> mapfield = 1;
}

Suppose as a c++ consumer i want to read this map field, what would be the api call neccessary for me to read these key and value from this map field into a string and int field in c++ ?

I tried to look for it here : https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#map-fields but could not find how the code on how to consume the map fields. Can someone help me with it ?

Thanks

1

There are 1 best solutions below

0
On

It works almost the same as std::map:

abc c;
(*c.mutable_mapfield())["abc"] = 2;   // set value with operator []

for (const auto &ele : c.mapfield()) { // iterate with range for
    cout << "key: " << ele.first << ", value: " << ele.second << "\n";
}