结构体重载运算符

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
#define inf 12345
using namespace std;
struct node{
int x1,x2,h;
bool operator < (const node& b) const{
return h < b.h; //或者写成 return this.h<b.h;
}
}a[20010];
/*
bool operator < (const node &b1,const node &b2)
{
return b1.h<b2.h;
}
*/
int main()
{
a[0].x1=1;a[0].x2=1;a[0].h=1;
a[1].x1=2;a[1].x2=2;a[1].h=2;
a[2].x1=3;a[2].x2=3;a[2].h=3;
sort(a, a+3);
for(int i=0;i<3;i++)
{
cout<<a[i].x1<<" "<<a[i].x2<<" "<<a[i].h<<endl;
}
}

//输出:
//1 1 1
//2 2 2
//3 3 3

————————————————
版权声明:本文为CSDN博主「flyzer」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/flyzer/article/details/80273334

原文地址:https://www.cnblogs.com/t-s-y/p/12433650.html