Do values with same hash go in same bucket of std::unordered_map?

。_饼干妹妹 提交于 2019-12-23 09:30:37

问题


If two keys for a std::unordered_map have the same hash value, is it guaranteed by the standard that they will go into the same bucket? We are assuming that the keys are not equal according to the template equality predicate, they only have the same hash value.

Bonus question: If same hash does not imply same bucket, then what is the purpose of being able to traverse buckets individually?


回答1:


Objects with the same hash are put into the same bucket by unordered associative containers. Consequently, two equal objects must have the same hash.

23.2.5 paragraph 8:

The elements of an unordered associative container are organized into buckets. Keys with the same hash code appear in the same bucket.

Bonus question: Why might you want to traverse buckets individually?

Bonus answer: Because you want to process the container's contents in parallel. The bucket iterators are independent of each other, so each thread can process a bucket without co-ordination (provided no new entries are added to the container). And the buckets should be roughly the same size, so they provide a convenient parallelisation quantum.



来源:https://stackoverflow.com/questions/12901346/do-values-with-same-hash-go-in-same-bucket-of-stdunordered-map

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