在许多场合下,使用c++重载运算符是很方便的,下面是重载系统小于号,改为大于号的代码,可以应用于大根堆(c++默认)与小根堆转换等问题
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int x;
} ;
bool operator<(const node &a,const node &b)
{
return a.x>b.x;
}
node a[15]={0,1,4,5,3,2,6,7,8,10,9};
int main()
{
sort(a+1,a+10+1);
for(int i=1;i<=10;i++)
printf("%d ",a[i].x);
return 0;
}
程序输出
10 9 8 7 6 5 4 3 2 1
未重载情况下输出
1 2 3 4 5 6 7 8 9 10