Does copy of unordered_map will have exactly same buckets

房东的猫 提交于 2019-12-08 03:24:12

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/51592794/does-copy-of-unordered-map-will-have-exactly-same-buckets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!