**頭文件 **#include <algorithm>
使用方法
默認 sort(首指針,末指針) // 默認升序
sort(首指針,末指針,cmp); // cmp 爲自己設定的排序規則
數組排序
int a[100];
bool cmp(int a,int b)
{
return a>b; // 降序
// return a<b; 升序
}
sort(a,a+100,cmp);
結構體排序
// **第一個例子**
struct student
{
char name[10];
int score; // 學生按成績排序
}s[100];
bool cmp(student a,student b)
{
return a.score>b.score; //降序
return a.score<b.score; // 升序
}
sort(a,a+100,cmp);
第二個例子
struct student
{
int math;
int English; // 數學成績高者 排名越高 數學成績相等時 英語成績高者 排名高
}s[100];
bool cmp(student a,student b)
{
if(a.math==b.math)
return a.Englsih>b.English;
return a.math>b.math;
}
對象排序
根據自己 class A
A a[100];
自己重載一個 大於號或者小於號