这是自定义类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(在集合中)?