结构体相关
2025-08-09 16:14:08
发布于:浙江
1阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
// 运算符重载
struct Point {
int x, y;
// 构造函数
Point() : x(0), y(0) {}
Point(int x, int y) : x(x), y(y) {}
// 算术运算符
Point operator+(const Point& other) const {
return Point(x + other.x, y + other.y);
}
friend Point operator-(const Point& a, const Point& b) {
return Point(a.x - b.x, a.y - b.y);
}
// 比较运算符
bool operator==(const Point& other) const {
return x == other.x && y == other.y;
}
bool operator<(const Point& other) const {
return x < other.x || (x == other.x && y < other.y);
}
// 赋值运算符
Point& operator=(const Point& other) {
x = other.x;
y = other.y;
return *this;
}
// 输入输出
friend ostream& operator<<(ostream& os, const Point& p) {
os << "(" << p.x << ", " << p.y << ")";
return os;
}
friend istream& operator>>(istream& is, Point& p) {
is >> p.x >> p.y;
return is;
}
};
int main() {
return 0;
}
这里空空如也
有帮助,赞一个