检查地图中地图中是否存在元素 [英] Checking whether an element exist or not in the map within map

查看:63
本文介绍了检查地图中地图中是否存在元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个格式为 map< key1,map< key2,value>的地图.> :

例如:我将强度值存储在以下地图的二维坐标(x,y)处:

For Example: I am storing the intensity value at 2-D co-ordinate(x,y) in following map:

map<int, map<int, double> > intensityValue;

现在,我要检查此地图中是否存在坐标(x,y)处的强度值.我知道的一种方法是检查:

Now, I want to check whether intensity value at co-ordinate (x,y) exist in this map or not. One way that I know is to check :

if(intensityvalue[x][y] >=0)

在这种情况下,如果地图中不存在 intensityValue [x] [y] ,则在检查后它将自动在地图中插入 intensityValue [x] [y] 映射我不想要的.

in this case, if intensityValue[x][y] does not exist in map then after checking it will automatically insert intensityValue[x][y] in the map which I don't want.

请提出一种有效的方法,以便我可以检查 intensityValue [x] [y] 是否已存在于地图中,而无需将其插入地图中.

Please suggest an efficient way, so that I can check whether intensityValue[x][y] already exist in the map or not without inserting it in the map.

推荐答案

您可以使用 std :: map :: find 以及短路评估:

You can use std::map::find together with short-circuit evaluation:

bool foundXY = instensityValue.find(x) != intensityValue.end() &&
               intensityValue[x].find(y) != intensityValue[x].end();

std :: map :: count :

bool foundXY = instensityValue.count(x) && intensityValue[x].count(y)

这篇关于检查地图中地图中是否存在元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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