Can we store unordered_map::iterator?

前端 未结 2 401
暗喜
暗喜 2021-01-17 19:25

Reference http://www.careercup.com/question?id=17188673 by chetan.j9

void Insert( string s ) {
    if( IsElementPresent(s) )
        return;

    myMap[s] =          


        
2条回答
  •  爱一瞬间的悲伤
    2021-01-17 20:03

    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.

提交回复
热议问题