The find
member function of std::map
specializations returns a past-the-end iterator if the key does not exist.
So you need to compare to the container’s end()
:
auto f = cnt.find("asdasd");
if (f != cnt.end()) {
std::cout << f->second;
}
solved Program crashes on trying to access the key that does not exist in map [duplicate]