有两种方法,一种直接法,一种间接法
先看直接法,要用到map的find()方法
map<int, string> i2s_map; auto pos=i2s_map.find(k); flag= (pos==i2s_map.end())? 0:1;
flag=0,表示map中没有要查找的key值
flag=1,表示map中存在查找的key值,且迭代器pos指向该key值
判断某key是否存在也可以使用map的count方法来间接判定
count接受一个参数key值,返回map中key值为给定值的元素总数
1 map<int, string> i2s_map; 2 int mount = i2s_map.count(100);
如果mount=0,则map中不存在key值为100的元素,反之则存在