简体   繁体   English

如何在 C++ 中迭代谷歌 protobuf 地图?

[英]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?我应该如何遍历地图以获取所有键和相应的值?

You iterate through a google::protobuf::Map in exactly the same way as a std::unordered_map .您以与std::unordered_map完全相同的方式遍历google::protobuf::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如果您有 C++17 编译器,则可以使用结构化绑定进一步拆分

for (auto & [key, value] : map)
{
    doSomethingWithKey(key);
    doSomethingWithValue(value);
}

From the documentation文档

A google::protobuf::Map is a special container type used in protocol buffers to store map fields. google::protobuf::Map 是一种特殊的容器类型,用于协议缓冲区中存储地图字段。 As you can see from its interface below, it uses a commonly-used subset of std::map and std::unordered_map methods从下面的界面可以看出,它使用了 std::map 和 std::unordered_map 方法的常用子集

and

google::protobuf::Map supports the same iterator API as std::map and std::unordered_map. google::protobuf::Map 支持与 std::map 和 std::unordered_map 相同的迭代器 API。 If you don't want to use google::protobuf::Map directly, you can convert a google::protobuf::Map to a standard map by doing the following:如果您不想直接使用 google::protobuf::Map,则可以通过执行以下操作将 google::protobuf::Map 转换为标准地图:

So either of the two approaches shown in the example code below should work:因此,下面示例代码中显示的两种方法中的任何一种都应该有效:

int main ()
{
  std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  // show content:
  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

  for (auto& x : mymap)
    std::cout << x.first << " => " << x.second << '\n';

  return 0;
}

to be able to do structured binding for with protobuf Map:能够DO结构结合for具有protobuf的地图:

for (auto & [key, value] : map) {
}

you need to include this bit of code that tells the compiler to to do it:你需要包含这段代码来告诉编译器这样做:

namespace std {
    template<typename TK, typename TV>
    class tuple_size<google::protobuf::MapPair<TK,TV>> : public std::integral_constant<size_t, 2> { };

    template<size_t I, typename TK, typename TV> 
    struct tuple_element< I, google::protobuf::MapPair<TK, TV>> { };
    template<typename TK, typename TV> struct tuple_element<0, google::protobuf::MapPair<TK, TV>> { using type = TK; };
    template<typename TK, typename TV> struct tuple_element<1, google::protobuf::MapPair<TK, TV>> { using type = TV; };

    template<int I, typename TK, typename TV>
    auto get(const google::protobuf::MapPair<TK, TV>& x) {
        if constexpr (I == 0) return x.first;
        if constexpr (I == 1) return x.second;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM