如果我只想指定一个哈希函数,我应该传递给unordered_map的bucket count参数?

时间:2022-02-24 18:51:43

C++11's unordered_map's default constructor looks like this:

C ++ 11的unordered_map的默认构造函数如下所示:

explicit unordered_map( size_type bucket_count = /*implementation-defined*/,
                    const hasher& hash = hasher(),
                    const key_equal& equal = key_equal(),
                    const allocator_type& alloc = allocator_type() );

I want to create an unordered_map with a custom hasher function, but it's the second argument to the constructor.

我想创建一个带有自定义hasher函数的unordered_map,但它是构造函数的第二个参数。

What bucket count should I use? Is there a magic value I can use to tell the container to decide for itself? Otherwise, is there a heuristic I can use to guesstimate a good bucket number based on something like the number of keys I expect my map to contain? Should I even care?

我应该使用什么桶数?是否有一个神奇的价值我可以用来告诉容器自己决定?否则,是否有一种启发式方法可以根据我期望我的地图包含的键数来猜测一个好的桶号?我应该关心吗?

2 个解决方案

#1


14  

I wouldn't worry too much about it.

我不会太担心它。

The container guarantees the bucket count will be at least the value you provide, i.e. it will increase it if needed. You could pass zero as the bucket count and the implementation will either do something like std::max(count, 10) and override the zero value, or it will just rehash on the first insertion.

容器保证桶数至少是您提供的值,即如果需要它将增加它。你可以传递零作为桶计数,并且实现将执行类似std :: max(count,10)的操作并覆盖零值,或者它将在第一次插入时重新执行。

Another alternative would be to copy the value from a default-constructed object:

另一种方法是从默认构造的对象复制值:

H hasher;
unordered_map<K,T,H,P> m{ unordered_map<K,T,H,P>{}.bucket_count(), hasher };

This will set the bucket count to whatever the implementation's default is (but does require the H hash function type to be DefaultConstructible.)

这会将桶数设置为实现的默认值(但需要H哈希函数类型为DefaultConstructible。)

FWIW GCC's unordered_map uses 10 as the default for the constructor you showed (so that's probably a reasonable default too) and uses 0 for the constructors taking a pair of iterators or an initializer_list.

FWIW GCC的unordered_map使用10作为你显示的构造函数的默认值(所以这也可能是一个合理的默认值),并且对于带有一对迭代器或初始化列表的构造函数使用0。

#2


2  

One of the template parameters for unordered_map is the hash function. If you specify your hash function object there you can leave the constructor parameters at their default settings.

unordered_map的模板参数之一是散列函数。如果指定了哈希函数对象,则可以将构造函数参数保留为默认设置。

#1


14  

I wouldn't worry too much about it.

我不会太担心它。

The container guarantees the bucket count will be at least the value you provide, i.e. it will increase it if needed. You could pass zero as the bucket count and the implementation will either do something like std::max(count, 10) and override the zero value, or it will just rehash on the first insertion.

容器保证桶数至少是您提供的值,即如果需要它将增加它。你可以传递零作为桶计数,并且实现将执行类似std :: max(count,10)的操作并覆盖零值,或者它将在第一次插入时重新执行。

Another alternative would be to copy the value from a default-constructed object:

另一种方法是从默认构造的对象复制值:

H hasher;
unordered_map<K,T,H,P> m{ unordered_map<K,T,H,P>{}.bucket_count(), hasher };

This will set the bucket count to whatever the implementation's default is (but does require the H hash function type to be DefaultConstructible.)

这会将桶数设置为实现的默认值(但需要H哈希函数类型为DefaultConstructible。)

FWIW GCC's unordered_map uses 10 as the default for the constructor you showed (so that's probably a reasonable default too) and uses 0 for the constructors taking a pair of iterators or an initializer_list.

FWIW GCC的unordered_map使用10作为你显示的构造函数的默认值(所以这也可能是一个合理的默认值),并且对于带有一对迭代器或初始化列表的构造函数使用0。

#2


2  

One of the template parameters for unordered_map is the hash function. If you specify your hash function object there you can leave the constructor parameters at their default settings.

unordered_map的模板参数之一是散列函数。如果指定了哈希函数对象,则可以将构造函数参数保留为默认设置。