STL中sort、priority_queue、map、set的自定义比较函数

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 ",array[i]); printf("\n");
    return 0 ;
}

2、priority_queue

#include <stdio.h>
#include <queue>
using namespace std ;

struct cmp{
    bool operator()( const int& a , const int& b )const{
        return a < b ;      //大顶堆
    }
};
struct Node{
    int x, y ;
    Node(int _x, int _y ):x(_x),y(_y){}
    bool operator <(const Node& n1)const{
        if( x < n1.x )    return true ;     //按照x为第一关键字由大到小排序
        else if( x == n1.x )   return y < n1.y  ;   //y为第二关键字由大到小排序
        else    return false ;
    }
} ;

int main(){
    //priority_queue<int> q ; //优先队列默认是less,大顶堆 ;
    //priority_queue<int,vector<int> ,cmp> q ;
    priority_queue< Node > q ;

    for(int i=0;i<10;i++)   q.push( Node( rand() , rand() ) );
    while( !q.empty() ){
        printf("%d %d\n",q.top().x , q.top().y ) ;
        q.pop() ;
    }
    return 0 ;
}
还可以在构造函数中进行比较运算符的初始化。
例如:
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
//小根堆
bool cmp(int a,int b) { return a>b; } int main() { int ia[9]={0,1,2,3,4,8,9,3,5}; priority_queue<int,vector<int>,bool (*)(int,int)> ipq(ia,ia+9,cmp); //默认参数要从左到右开始指定 cout<<"size=" <<ipq.size()<<endl; for(int i=0;i<ipq.size();++i) cout<<ipq.top()<<' '; cout<<endl; while(!ipq.empty()) { cout<<ipq.top()<<' '; ipq.pop(); } cout<<endl; }

3、map

 

#include<stdio.h>
#include <map>
using namespace std;

struct cmp{
    bool operator()( const int& a, const int& b ) const{
        return a < b ; //从小到大;
    }
};
int main(){
    //map<int, int,greater<int> > mp ;  //从大到小
    map<int , int ,cmp> mp ;
    for(int i=0;i<10;++i)   mp.insert( map<int,int,cmp >::value_type(rand() ,i) ) ;
    map<int, int, cmp >::iterator it = mp.begin() ;
    for( ;it!=mp.end() ;it++)   printf("%d %d\n",(*it).first , (*it).second );
    return 0 ;
}

 

4、set

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

struct cmp{
    bool operator()( const int& a , const int& b )const{
        return a < b ; //从小到大
    }
} ;

int main(){
    //set<int > s ;
    set<int,cmp> s ;
    for(int i=0;i<10;i++)   s.insert( rand() ) ;
    set<int,cmp>::iterator it = s.begin() ;
    for( ; it!=s.end();it++)
        printf("%d\n",*it);
    return 0 ;
}
令一种比较函数的声明方式,在构造函数中初始化:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

struct cmp{
    bool operator()( const int& a , const int& b )const{
        return a < b ; //从小到大
    }
} ;
bool mycmp(int a,int b)
{
    return a<b;
}
int main(){
    //set<int > s ;
    set<int,decltype(mycmp)*> s(mycmp) ;
    for(int i=0;i<10;i++)   s.insert( rand() ) ;
    set<int,cmp>::iterator it = s.begin() ;
    for( ; it!=s.end();it++)
        printf("%d\n",*it);
    return 0 ;
}
 
posted @ 2015-04-09 13:03  Jessica程序猿  阅读(1773)  评论(0编辑  收藏  举报