Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through a google protobuf map in C++?

I have my map in the following form:

const google::protobuf::Map<string, nrtprofile::RedisNewsMetric> map=redisNewsMessage.ig();

How should I iterate through the map to get all the keys and corresponding values?

like image 634
NITIKA KHATKAR Avatar asked Dec 08 '22 14:12

NITIKA KHATKAR


1 Answers

You iterate through a google::protobuf::Map in exactly the same way as a std::unordered_map.

for (auto & pair : map)
{
    doSomethingWithKey(pair.first);
    doSomethingWithValue(pair.second);
}

If you have a C++17 compiler, you can use a structured binding to split that further

for (auto & [key, value] : map)
{
    doSomethingWithKey(key);
    doSomethingWithValue(value);
}
like image 90
Caleth Avatar answered Jan 04 '23 04:01

Caleth