頭文件:#include <algorithm>using namespace std;1.預設的sort函數是按升序排。對應於1)sort(a,a+n); //兩個參數分別為待排序數組的首地址和尾地址2.可以自己寫一個cmp函數,按特定意圖進行排序。對應於2)例如:int cmp( const i ...
頭文件:
#include <algorithm>
using namespace std;
1.預設的sort函數是按升序排。對應於1)
sort(a,a+n); //兩個參數分別為待排序數組的首地址和尾地址
2.可以自己寫一個cmp函數,按特定意圖進行排序。對應於2)
例如:
int cmp( const int &a, const int &b ){
if( a > b )
return 1;
else
return 0;
}
sort(a,a+n,cmp);
是對數組a降序排序
又如:
int cmp( const POINT &a, const POINT &b ){
if( a.x < b.x )
return 1;
else
if( a.x == b.x ){
if( a.y < b.y )
return 1;
else
return 0;
}
else
return 0;
}
sort(a,a+n,cmp);
是先按x升序排序,若x值相等則按y升序排