c++ 中的set为什么调用count后总返回1?

这是自定义类Point代码

struct Point
{
    int x;
    int y;

    explicit Point(int x = 0, int y = 0) : x(x), y(y)
    {}

    bool operator==(const Point &rhs) const
    {
        return x == rhs.x &&
               y == rhs.y;
    }

    bool operator!=(const Point &rhs) const
    {
        return !(rhs == *this);
    }

    bool operator<(const Point &rhs) const
    {
        return x < rhs.x && y < rhs.y;
    }

    bool operator>(const Point &rhs) const
    {
        return rhs < *this;
    }

    bool operator<=(const Point &rhs) const
    {
        return !(rhs < *this);
    }

    bool operator>=(const Point &rhs) const
    {
        return !(*this < rhs);
    }

    static bool isValid(Point pos, Point end)
    {
        Point Zero;
        return pos >= Zero && pos <= end;
    }

    friend ostream &operator<<(ostream &os, const Point &point)
    {
        os << "x: " << point.x << " y: " << point.y;
        return os;
    }
};

这是main函数

int main()
{
    Point a(2, 3);
    Point b(1, 3);
    set<Point> myset;
    myset.insert(a);
    cout << (a == b) << endl;
    cout << myset.count(b) << endl;

}

调试及运行结果如下图

图片说明

图片说明

图片说明

非常奇怪,明明set中两个元素不相等,调用count时,却返回1(在集合中)?

逻辑判断不对,你那么写x y只要有一个相同,就判断相等了

    bool operator==(const Point &rhs) const
    {
        return x == rhs.x ||
               y == rhs.y;
    }

(或者 Point b(1, 4); 两个都不同)
这样就可以了

问题解决的话,请点下采纳