在地图上使用find< pair,int> [英] Using find on map<pair, int>

查看:137
本文介绍了在地图上使用find< pair,int>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您的地图定义为



<$ c $如何使用 const_iterator c> typedef std :: pair< int,int> MyPair;



map< MyPair,int> MyData;



,其中 b
$ b

如果只是 map< int,int> ,我知道如何使用 const_iterator like



typedef map< int,int> MyMap;



MyMap :: const_iterator it =
MyMap.find(0);

解决方案

如果你不使用C ++ 11 ,最方便的还是为地图类型做一个 typedef

  typedef std :: map< MyPair,int> map_type; 

然后

  map_type :: const_iterator it = MyMap.find(make_pair(0,0)); 

(我也更改了传递给的参数 int 与您的地图不兼容。)



如果您使用C ++ 11 ,你也可以做

  auto it = MyMap.find(make_pair ); 


How do you use find with a const_iterator if you have a map defined as

typedef std::pair<int, int> MyPair;

map<MyPair, int> MyMap;

with the pair defined as the key.

If it was just map<int, int>, I know how to use a const_iterator like

typedef map<int, int> MyMap;

MyMap::const_iterator it = MyMap.find(0);

etc..

解决方案

If you are not using C++11, the most convenient is to also do a typedef for the map type:

typedef std::map<MyPair, int> map_type;

And then

map_type::const_iterator it = MyMap.find(make_pair(0, 0));

(I also changed the parameter passed to find, as a bare int is not compatible with your map).

If you are using C++11, you can also do simply

auto it = MyMap.find(make_pair(0, 0));

这篇关于在地图上使用find&lt; pair,int&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆