Arkady
Arkady

Reputation: 2207

Does copy of unordered_map will have exactly same buckets

As far as I understand, amount of buckets in unordered_map increases accidentally while filling of unordered_map.

If I perform copy of unordered_map (to another unordered_map) it is guaranteed there will be exactly same pairs. But will they be in the same buckets? Will amount of buckets will be the same?

I don't know bucket's creation mechanism, and didn't find short explanation, how it have to be implemented in standard. But if buckets amount may rely on sequence of insertions, allocation and etc, then after copying we may get different amount of buckets, or different distribution in there (even if items will be the same). Is it true? Both for boost's implementation and for gcc's standard implementation?

Upvotes: 2

Views: 123

Answers (1)

Caleth
Caleth

Reputation: 63402

The max load factor, but not the "current" load factor are specified as being copied when an unordered_map is copied.

Both the entry for copy construction and copy assignment include the following

In addition to the requirements of Table 64, copies the hash function, predicate, and maximum load factor.

[unord.req]

In general this means there may be a different count of buckets, and thus a different distribution of elements into buckets in a copy.

Upvotes: 3

Related Questions