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;