I am experiencing the following behavior: I create a map, do a find on the key and delete the map entry. After erase, I print the elements using the iterator and I expected it to dump core but it works.
Why does it work?
typedef std::pair<std::string, int> pair; std::map<pair, int> nameidCntMap; pair pair1("ABC", 139812); pair pair2("XYZ", 139915); pair pair3("PQR", 139098); nameidCntMap.insert(std::make_pair(pair1, 1)); nameidCntMap.insert(std::make_pair(pair2, 1)); nameidCntMap.insert(std::make_pair(pair3, 1)); std::map<pair, int>::iterator it = nameidCntMap.find(pair1); if (it != nameidCntMap.end()) { symsrcidCntMap.erase(it); std::cout<<"Pair::first: "<<it->first.first << "Pair::second: "<<it->first.second<<"map second:"<<it->second<<std::endl; }
itonce it's no longer valid invokes undefined behavior. A core dump is a possible outcome, but anything could happen, including printing the values.