【问题标题】:Does boost::unordered_multimap resizeboost::unordered_multimap 是否调整大小
【发布时间】:2017-06-09 04:55:02
【问题描述】:

在初始化 boost::unordered_multimap 时,我们定义了 hash map 的大小。

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 大小的最佳做法是什么?

【问题讨论】:

  • 是的,它会自动调整大小 - 如果不这样做,它将几乎无法使用。最佳做法是在初始化时对大小进行有根据的猜测,或者如果不可能,让它选择自己的大小。
  • 什么时候决定调整大小?
  • 如果不研究代码本身,我无法确定,但我相信作者选择了一个合理的折衷方案。
  • @JoeC,据我了解,您引用了n 参数,它是多个桶,它不是“哈希映射的大小”的同义词。此外,它是“至少”个桶数。
  • 还有unordered_multimap现在在标准库中

标签: c++ boost hashtable


【解决方案1】:

它会自动完成,as documented:

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

但请记住,收缩时它不会重新散列:Why does C++11/Boost `unordered_map` not rehash when erasing?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    相关资源
    最近更新 更多