为什么具有自定义哈希函数和自定义 class 的 unordered_set 需要初始数量的桶?
Why does a unordered_set with a custom hash function and custom class need an initial number of buckets?
基本上我的问题是,为什么不能编译?
#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 以证明函子版本不需要初始桶数。
那是因为没有这样的构造函数。
only unordered_set constructor that takes one parameter 是采用自定义分配器实例的那个,而不是自定义哈希函数。
P.S。您无法在自定义哈希函数中将 hash
初始化为 0。这带来了更高的风险 of nasal demons。你应该解决这个问题。
作为旁注,如果您可以访问 C++20,则可以在不构造 lambda 的情况下执行 decltype
,让 std::unordered_set
默认构造它。
using hash_function = decltype([](const std::vector<int>& v) {
size_t hash = 0;
for (int i = 0; i < v.size(); ++i) {
hash += v[i] + 31 * hash;
}
return hash;
});
std::unordered_set<std::vector<int>, hash_function> s();
基本上我的问题是,为什么不能编译?
#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 以证明函子版本不需要初始桶数。
那是因为没有这样的构造函数。
only unordered_set constructor that takes one parameter 是采用自定义分配器实例的那个,而不是自定义哈希函数。
P.S。您无法在自定义哈希函数中将 hash
初始化为 0。这带来了更高的风险 of nasal demons。你应该解决这个问题。
作为旁注,如果您可以访问 C++20,则可以在不构造 lambda 的情况下执行 decltype
,让 std::unordered_set
默认构造它。
using hash_function = decltype([](const std::vector<int>& v) {
size_t hash = 0;
for (int i = 0; i < v.size(); ++i) {
hash += v[i] + 31 * hash;
}
return hash;
});
std::unordered_set<std::vector<int>, hash_function> s();