确实会提升::unordered_multimap调整大小

Does boost::unordered_multimap resize

本文关键字:multimap 调整 unordered      更新时间:2023-10-16

初始化 boost::unordered_multimap 时,我们定义哈希映射的大小。

explicit unordered_multimap(size_type n = implementation-defined, 
hasher const& hf = hasher(), 
key_equal const& eq = key_equal(), 
allocator_type const& a = allocator_type());
Constructs an empty container with at least n buckets, using hf as the hash function, eq as the key equality predicate, a as the allocator and a maximum load factor of 1.0.

当哈希映射几乎已满时,它会调整大小吗?它似乎没有这样的 API。它会自动完成吗?定义unordered_multimap大小的最佳做法是什么?

它将自动执行此操作,如文档所示:

存储桶的数量可以通过调用插入或调用 rehash 来自动增加。

但请记住,它在缩小时不会重新散列:为什么C++11/Boost 'unordered_map'在擦除时不会重新散列?

您可以通过手动强制降低负载系数并将最大负载系数设置得足够高(http://www.boost.org/doc/libs/1_64_0/doc/html/boost/unordered_map.html#idp776321712-bb、http://www.boost.org/doc/libs/1_64_0/doc/html/boost/unordered_map.html#idp776318000-bb)来防止不必要的重新哈希