我想使用 stdext::hash_set
用于自定义类型。
事实上我知道怎么做了,但我不确定它是否正确(它是可编译的,但看起来有点脏)。
代码如下:
// This is my custom type
struct Point
{
Point(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
int x, y, z;
bool operator< (const Point& other) const
{
if (x != other.x) return x < other.x;
if (y != other.y) return y < other.y;
return z < other.z;
}
};
// helper class
struct PointHashCompare {
// value is copied from MS sources
static const int bucket_size = 1;
size_t operator() (const Point& p) const {
return p.x * 31 * 31 + p.y * 31 + p.z;
}
bool operator() (const Point& a, const Point& b) const {
return a < b;
}
};
变量声明如下:
stdext::hash_set<Point, PointHashCompare> hSet;
PointHashCompare
中的bucket_size
是什么意思?
是否存在任何 Microsoft 文档来解释 bucket_size
和对其值的建议以及为什么需要它?
(我可以假设这与哈希表实现的内部结构有关,但可以使用不同的方法,并且它们也可以在不同的 VC++ 版本中更改)
我也在考虑切换到std::unordered_set
,但现在我想知道如何使用stdext::hash_set
进行管理
谢谢!
最佳答案
The integer constant
bucket_size
specifies the mean number of elements per "bucket" (hash-table entry) that the container should try not to exceed. It must be greater than zero. The value supplied byhash_compare
is 4.
更多信息请参见页面 hash_compare Class
关于c++ - 如何在 VC++ 2010 中将 stdext::hash_set 用于自定义类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9630056/