C ++: is std :: unordered_map a node guarantee?

What is the typical layout of std::unordered_map<K, V> ? Are the objects K and V stored in the buckets themselves, or do the buckets store pointers on nodes containing keys and values?

I am trying to understand the consequences of using std::unordered_map<K, V> compared to std::unordered_map<K, V*> . Assuming I’m only ever going to look at values, is there any reason to prefer the latter, even if the values ​​are quite large? The only reason I can imagine is that the values ​​are stored in a row in buckets and should be redistributed every time you look at the container again.

Is there anything in the standard to ensure that this does not happen?

+5
source share
1 answer

[unord.req] / 8 :

Rehashing cancels iterators, reorders between elements, and changes that include bucket elements but do not invalidate pointers or element references.

The fact that pointers and references to elements are not invalid by renaming (or inserting / deleting, see / 13), to a large extent, means that they must be a node.

C ++ 17 even provides node descriptors so you can wrap nodes between two unordered_map s.

+12
source

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


All Articles