简体   繁体   中英

C++: is std::unordered_map guaranteed to be node-based?

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

I'm trying to figure out the performance implications of using std::unordered_map<K, V> versus std::unordered_map<K, V*> . Assuming I only ever emplace and look up values, is there any reason to prefer the latter, even if the values are quite large? The only reason I can imagine is if the values are stored in-line in buckets, and need to be re-allocated each time the container is rehashed.

Is there anything in the standard that guarantees this won't happen?

[unord.req]/8 :

Rehashing invalidates iterators, changes ordering between elements, and changes which buckets elements appear in, but does not invalidate pointers or references to elements.

The fact that pointers and references to elements are not invalidated by rehashing (or insertion/deletion, see /13) pretty much means that they have to be node based.

C++17 even exposes node handles so that you can transfer nodes between two unordered_map s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM