c++ - 如何在 VC++ 2010 中将 stdext::hash_set 用于自定义类型?

标签 c++ visual-c++ stl hashtable containers

我想使用 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 by hash_compare is 4.

更多信息请参见页面 hash_compare Class

关于c++ - 如何在 VC++ 2010 中将 stdext::hash_set 用于自定义类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9630056/

相关文章:

visual-c++ - Visual Studio 2012 update 3 编译器错误 - 不调用 dtor

C++ vector 引用作品很有趣

c++ - 将子 vector 合并/展平为单个 vector c++(将 2d 转换为 1d)

c++ - 以基类为值的 std::map 中的继承

c++ - 何时使用 const char * 以及何时使用 const char []

c++ - 在 MSVC 的 Debug模式下分配给 std::future 时崩溃

c++ - 这个c++程序中栈是怎么一步步变化的?

c++ - 声明一个 C++ 类而不在当前翻译单元中定义它

dll - 是否有与 gcc --kill-at 等效的 Visual C++?

c++ - operator[] 查找模板基类