小编SyT*_*eSy的帖子

如何将unordered_set与自定义结构一起使用?

我想使用unordered_set带有自定义的struct。在我的情况下,自定义struct表示欧氏平面中的2D点。我知道应该定义一个哈希函数和比较器运算符,并且我已经做到了,如下面的代码所示:

struct Point {
    int X;
    int Y;

    Point() : X(0), Y(0) {};
    Point(const int& x, const int& y) : X(x), Y(y) {};
    Point(const IPoint& other){
        X = other.X;
        Y = other.Y;
    };

    Point& operator=(const Point& other) {
        X = other.X;
        Y = other.Y;
        return *this;
    };

    bool operator==(const Point& other) {
        if (X == other.X && Y == other.Y)
            return true;
        return false;
    };

    bool operator<(const Point& other) {
        if (X < other.X …
Run Code Online (Sandbox Code Playgroud)

c++ struct set unordered-set c++11

5
推荐指数
3
解决办法
2492
查看次数

标签 统计

c++ ×1

c++11 ×1

set ×1

struct ×1

unordered-set ×1