Suppose I have a hash_map and a code like
// i is an iterator i = hash_map.erase(i)
But GCC’s STL doesn’t return iterator in erase, but a void. Now is a code like
hash_map.erase(i++)
safe (i.e. does not invalidate the iterator or does any other unexpected or unpleasant things)? Please note this is a hash_map.
Yes, this is safe, because the value of
iwill have been set to the next value, before the current value is erased.According to the SGI documentation about hashed containers invalidation does not occur for non-erased elements, nor even for resizing (there is no word on whether insertions cause resizing, so to be careful I admit that as a possibility)—but in the latter case, the iteration order will be changed. But this doesn’t apply here, unless you go out of your way to resize the container during traversal or something. 🙂