Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does unordered_map copy/release contained objects if there is insert/remove/rehash operation?

Tags:

c++

stl

boost

I wanna store small objects in unordered_map, just wondering if it may copy/release contained objects if there is any insert/remove/rehash operation? I think unordered_map uses link list to store key/value pair, it should not need to copy/release objects like vector for memory reallocation.

like image 433
poordeveloper Avatar asked Dec 29 '11 05:12

poordeveloper


1 Answers

C++11 standard: § 23.2.5/8

The elements of an unordered associative container are organized into buckets. Keys with the same hash code appear in the same bucket. The number of buckets is automatically increased as elements are added to an unordered associative container, so that the average number of elements per bucket is kept below a bound. Rehashing invalidates iterators, changes ordering between elements, and changes which buckets elements appear in, but does not invalidate pointers or references to elements. For unordered_multiset and unordered_multimap, rehashing preserves the relative ordering of equivalent elements.

In short for unordered_map,
In case of Insert/Erase operations,

  • All iterators are invalidated when rehashing occurs, but references remain unaffected.
like image 100
Alok Save Avatar answered Oct 13 '22 00:10

Alok Save