STL中,sort的默认排序为less,也就是说从小到大排序;priority_queue默认是less,也就说大顶堆;map默认是less,也就说用迭代器迭代的时候默认是小的排在前面;set默认是less,也就是说用迭代器迭代的时候是从小到大排序的。
1、sort
#include <stdio.h>
#include <algorithm>
#include <functional>
using namespace std;
bool comp(const int& a, const int& b ){
return a < b ; //从小到大
}
struct cmp{
bool operator()( const int& a , const int& b ) const{
return a < b ; //从小到大
}
} ;
int main(){
int array[] = {1 ,5 ,4, 10 , 3, 6 } ;
sort( array , array+6 ) ; //以默认的less<int>()排序
sort( array , array+6 , greater<int>() ) ; //从大到小排序
sort( array , array+6 , comp ) ;
sort( array , array+6 , cmp() ) ;
for(int i=0;i<6;++i) printf("%d ",arra