如何使用 const_iterator
查找如果您将 map 定义为
typedef std::pair<int, int> MyPair;
map<MyPair, int> MyMap;
与 pair
定义为键。
如果它只是 map<int, int>
, 我知道如何使用 const_iterator
喜欢
typedef map<int, int> MyMap;
MyMap::const_iterator it =
MyMap.find(0);
// etc..
最佳答案
如果您不使用 C++11,最方便的方法是也为 map 类型做一个 typedef
:
typedef std::map<MyPair, int> map_type;
然后
map_type::const_iterator it = MyMap.find(make_pair(0, 0));
(我还更改了传递给 find
的参数,因为裸 int
与您的 map 不兼容)。
如果你使用的是C++11,你也可以简单地做
auto it = MyMap.find(make_pair(0, 0));
关于c++ - 在 map 上使用查找<pair, int>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11246606/