Reference http://www.careercup.com/question?id=17188673 by chetan.j9
void Insert( string s ) {
if( IsElementPresent(s) )
return;
myMap[s] =
Insertion of an item invalidates all iterators only if a rehashing occurs (ie. if the new number of elements is greater than or equal to max_load_factor()*bucket_count()
). Otherwise no iterators are invalidated.
Removal of an item only invalidates the iterators to the removed element(s) not to other unrelated elements.
Source: std::unordered_map::insert and std::unordered_map::erase.
Of course these rules apply only to std::unordered_map
. Other containers may have different invalidation rules, it's up to you to look it up in the documentation.