C++判断map中key值是否存在

1、count函数

count函数用于统计key值在map中出现的次数,map的key不允许重复,因此如果key存在返回1,不存在返回0

if (mp.count(key) == 0)
    cout << "no this key" << endl;

2、find函数
如果key存在,则find返回key对应的迭代器,如果key不存在,则find返回尾后迭代器 .end()

if (mp.find(key) == mp.end())
    cout << "no this key" << endl;

猜你喜欢

转载自blog.csdn.net/chaokudeztt/article/details/114444493