check for a key in map c++ code example
Example 1: check if key exists in map c++
if ( m.find("f") == m.end() ) {
// not found
} else {
// found
}
Example 2: check if map key has alue cpp
#include<map>
int main(){
map<int,char> m;
char ch = '!';
if (m.find(ch) != m.end()) {
std::cout << "Key found";
} else {
std::cout << "Key not found";
}
return 0;
}
Example 3: c++ map key exists
if ( !(myMap.find("key") == myMap.end()) ) { // "key" exists
} else { // not found
}