I know that unordered_set might invalidate iterators when elements are inserted:
"If rehashing occurs (due to the insertion), all iterators are invalidated."
It is clear, because we have hash table with buckets, but why does unordered_multiset not invalidate iterators?
I think the implementation of unordered_set is almost identical with the implementation of unordered_multiset (hash table with buckets).
There is actually no difference in this respect between
std::unordered_setandstd::unordered_multiset.As you can see in the
unordered_multiset::insertdocumentation, it contains the exact same note asunordered_set::insert:BTW - they are also identical in the case where there is no rehashing:
Note that it doesn't mean the two containers will always behave the same when it comes to invalidating iterators - because the implementation has some freedom (tweaking buckets count etc. which can affect rehashing - see @TonyDelroy's comment below).