unordered_map - 哈希函数无效
unordered_map - Hash function has no effect
为什么下面的散列函数(returns 常量 0)似乎没有任何作用?
由于散列函数返回常量,我期望输出所有值都是 3。但是,它似乎将 std::vector
值唯一映射到唯一值,而不管我的散列函数是常量.
#include <iostream>
#include <map>
#include <unordered_map>
#include <vector>
// Hash returning always zero.
class TVectorHash {
public:
std::size_t operator()(const std::vector<int> &p) const {
return 0;
}
};
int main ()
{
std::unordered_map<std::vector<int> ,int, TVectorHash> table;
std::vector<int> value1({0,1});
std::vector<int> value2({1,0});
std::vector<int> value3({1,1});
table[value1]=1;
table[value2]=2;
table[value3]=3;
std::cout << "\n1=" << table[value1];
std::cout << "\n2=" << table[value2];
std::cout << "\n3=" << table[value3];
return 0;
}
获得的输出:
1=1
2=2
3=3
预期输出:
1=3
2=3
3=3
关于哈希,我错过了什么?
理智的哈希 table 实现不应该丢失信息,即使存在哈希冲突。有几种技术可以解决冲突(通常权衡运行时性能与数据完整性)。
显然,std::unordered_map
实现了它。
你误解了散列函数的用途:它不是用来比较元素的。在内部,映射将元素组织到桶中,哈希函数用于确定元素所在的桶。元素的比较是用另一个模板参数执行的,查看 unordered_map
模板的完整声明:
template<
class Key,
class T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator< std::pair<const Key, T> >
> class unordered_map;
散列器之后的下一个模板参数是关键比较器。要获得您期望的行为,您必须执行以下操作:
class TVectorEquals {
public:
bool operator()(const std::vector<int>& lhs, const std::vector<int>& rhs) const {
return true;
}
};
std::unordered_map<std::vector<int> ,int, TVectorHash, TVectorEquals> table;
现在您的地图将只有一个元素,您的所有结果都将是 3
。
添加谓词键比较器class。
class TComparer {
public:
bool operator()(const std::vector<int> &a, const std::vector<int> &b) const {
return true; // this means that all keys are considered equal
}
};
这样使用:
std::unordered_map<std::vector<int> ,int, TVectorHash, TComparer> table;
然后您的其余代码将按预期工作。
为什么下面的散列函数(returns 常量 0)似乎没有任何作用?
由于散列函数返回常量,我期望输出所有值都是 3。但是,它似乎将 std::vector
值唯一映射到唯一值,而不管我的散列函数是常量.
#include <iostream>
#include <map>
#include <unordered_map>
#include <vector>
// Hash returning always zero.
class TVectorHash {
public:
std::size_t operator()(const std::vector<int> &p) const {
return 0;
}
};
int main ()
{
std::unordered_map<std::vector<int> ,int, TVectorHash> table;
std::vector<int> value1({0,1});
std::vector<int> value2({1,0});
std::vector<int> value3({1,1});
table[value1]=1;
table[value2]=2;
table[value3]=3;
std::cout << "\n1=" << table[value1];
std::cout << "\n2=" << table[value2];
std::cout << "\n3=" << table[value3];
return 0;
}
获得的输出:
1=1
2=2
3=3
预期输出:
1=3
2=3
3=3
关于哈希,我错过了什么?
理智的哈希 table 实现不应该丢失信息,即使存在哈希冲突。有几种技术可以解决冲突(通常权衡运行时性能与数据完整性)。
显然,std::unordered_map
实现了它。
你误解了散列函数的用途:它不是用来比较元素的。在内部,映射将元素组织到桶中,哈希函数用于确定元素所在的桶。元素的比较是用另一个模板参数执行的,查看 unordered_map
模板的完整声明:
template<
class Key,
class T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator< std::pair<const Key, T> >
> class unordered_map;
散列器之后的下一个模板参数是关键比较器。要获得您期望的行为,您必须执行以下操作:
class TVectorEquals {
public:
bool operator()(const std::vector<int>& lhs, const std::vector<int>& rhs) const {
return true;
}
};
std::unordered_map<std::vector<int> ,int, TVectorHash, TVectorEquals> table;
现在您的地图将只有一个元素,您的所有结果都将是 3
。
添加谓词键比较器class。
class TComparer {
public:
bool operator()(const std::vector<int> &a, const std::vector<int> &b) const {
return true; // this means that all keys are considered equal
}
};
这样使用:
std::unordered_map<std::vector<int> ,int, TVectorHash, TComparer> table;
然后您的其余代码将按预期工作。