C++ map中key值存在情况判定

C++ map中key值存在情况判定

1、count函数

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

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

2、find函数

iterator find ( const key_type& key );

如果key存在,则find返回key对应的迭代器,如果key不存在,则find返回尾后迭代器 .end()
例:

if (testMap.find(key) == testMap.end())
    cout << "no this key" << endl;
posted @ 2021-02-11 20:55  TR_Goldfish  阅读(750)  评论(0编辑  收藏  举报