头文件:
#include <algorithm>
using namespace std;
1.默认的sort函数是按升序排)
sort(a,a+n); //两个参数分别为待排序数组的首地址和尾地址
2.可以自己写一个cmp函数,按特定意图进行排序
例如:
bool cmp( const int &a, const int &b ){
return a>b;
}
sort(a,a+n,cmp);
是对数组a降序排序
3.对结构体中多个关键字排序
struct rec
{
int w,d,g,m;
char s[35];
bool operator <(const rec &A)const{
if(A.d==d)
{
if(A.m==m)
{
return A.g<g;
}
return A.m<m;
}
return A.d<d;
}
}p[55];
4.按字典序给字符串排序
bool cmp2(const rec A, const rec B)
{
return strcmp(A.s,B.s) <= 0;//strcmp()参数必须是字符数组型,不能是字符串。
}
sort(p,p+n,cmp2);