Is the iteration order over std :: unordered_set elements a guarantee that they are always the same?

If you repeat the elements of std::unordered_set several times without changing the contents of the set (but perhaps reading it, calculating its size, etc.), is it guaranteed that the elements will be visited in the same order as every time?

+5
source share
1 answer

In the specific case, you say yes. Since the standard is clear when re-hashing occurs (and therefore reordering).

This only happens during insertion.

ยง 23.2.5 [unord.req]

9 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 automatically increases as elements are added to the disordered associative container, so that the average number of elements per bucket is kept below the border. Rehashing cancels iterators, reorders between elements and changes that bucket elements appear, but do not invalidate pointers or references to elements. For unordered_multiset and unordered_multimap, rehashing preserves the relative order of equivalent elements.

+4
source

Source: https://habr.com/ru/post/1245833/


All Articles