小编And*_*ndy的帖子

设置与无序集一起使用的自定义类 - 在集中找不到元素

我想做的是使 std::unordered_set 与我的自定义类 Vector2 一起使用成为可能 -包括搜索已经在 set 中的此类的对象的可能性

让我提供更多细节。我的 Vector2 类的标头(包括自定义哈希表)如下:

Class Vector2
{
private:
    static int lastID;
    const int id;
    int x;
    int y;

public:
    Vector2(int _x, int _y);
    ~Vector2();

    bool Vector2::operator==(const Vector2& other) const;

    int getId() const;
    int getX() const;
    int getY() const;
};

namespace std
{
    template<>
    struct hash<Vector2>
    {
        size_t
            operator()(const Vector2& obj) const
        {
            return hash<int>()(obj.getId());
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

此类的成员函数的实现很简单:

int Vector2::lastID = 0;

Vector2::Vector2(int _x, int _y) : id(lastID++)
{
    x = …
Run Code Online (Sandbox Code Playgroud)

c++ hash class find unordered-set

5
推荐指数
1
解决办法
1569
查看次数

标签 统计

c++ ×1

class ×1

find ×1

hash ×1

unordered-set ×1