To check if a particular key in the map exists, use the count
member function in one of the following ways:
m.count(key) > 0m.count(key) == 1m.count(key) != 0
The documentation for map::find
says: "Another member function, map::count
, can be used to just check whether a particular key exists."
The documentation for map::count
says: "Because all elements in a map container are unique, the function can only return 1 (if the element is found) or zero (otherwise)."
To retrieve a value from the map via a key that you know to exist, use map::at:
value = m.at(key)
Unlike map::operator[], map::at
will not create a new key in the map if the specified key does not exist.