map.erase( map.end() )?

end() is not an interator into the map. It's effectively 'one past the end' of the map.

The 'iterator' version wants an iterator to something in the map.
The 'key' version of erase does the lookup and protects itself against key not found, the iterator version assumes you aren't trying to break stuff.


Instead of the example given in a previous post...

MapType::iterator it = the_map.find ("new_key");

// Does not exist.
if (it == the_map.end()) {
  the_map.insert (std::make_pair ("new_key", 10));
}

which does two tree traversals, use...

pair<MapType::iterator, bool> rc = the_map.insert(make_pair("new_key", 0));
if (rc.second)
    rc.first.second = 10;

That way you do one tree traversal and you have the iterator ready to roll for other stuff.


For erase(key), the standard says that all elements with value key are removed. There may of course be no such values.

For erase(it) (where it is a std::map::iterator), the standard says that the element pointed to by it is removed - unfortunately, if it is end() it does not point to a valid element and you are off in undefined behaviour land, as you would be if you used end() for any other map operation. See section 23.1.2 for more details.