【发布时间】:2016-07-21 11:38:27
【问题描述】:
我正在为类 Coord(二维坐标)编写自定义散列函数。
是否可以更改以下哈希函数,使 b 为 unordered_set 哈希表中的当前桶数,并在桶数改变时更改?
namespace std
{
template <>
struct hash<Coord>
{
size_t operator()(const Coord &k) const
{
int b = 11;
int a1 = static_cast<int> (pow(b,(1.0/3.0)));
int a2 = static_cast<int> (pow(b,(2.0/3.0)));
return ((a1*k.getX() + a2*k.getY()) % b);
}
};
}
【问题讨论】:
-
std::cbrt(b)比pow(b,(1.0/3.0)更准确、更快。更具可读性
标签: c++ hash unordered-set