为什么带有自定义哈希函数和自定义类的unordered_set需要初始数量的存储桶?

问题描述 投票:0回答:1

基本上我的问题是,为什么不编译?

#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;


int main() {
    vector<int> v{1,2,3};
    auto hash_function=[](const vector<int>& v){
        size_t hash;
        for (int i = 0; i < v.size(); ++i) {
            hash+=v[i]+31*hash;
        }
        return hash;
        };

unordered_set<vector<int>, decltype(hash_function)> s(hash_function);
std::cout<<s.bucket_count();
std::cout<<"here";


}

但是如果我将unordered_set行更改为此,则>

unordered_set<vector<int>, decltype(hash_function)> s(10,hash_function);

会。为什么需要初始存储桶计数?使用lambda强制我添加初始存储桶计数似乎很奇怪,但使用函子却不会。请参见此处的示例:C++ unordered_set of vectors以证明函子版本不需要初始数量的存储桶。

基本上我的问题是,为什么不编译? #include #include #include 使用命名空间std; int main(){vector v {1,2,3}; ...

c++ unordered-set
1个回答
0
投票

这仅仅是因为没有这样的构造函数。

only unordered_set constructor that takes one parameter是采用自定义分配器的实例,而不是自定义哈希函数的实例。

© www.soinside.com 2019 - 2024. All rights reserved.