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

简介: STL中,sort的默认排序为less,也就是说从小到大排序;priority_queue默认是less,也就说大顶堆;map默认是less,也就说用迭代器迭代的时候默认是小的排在前面;set默认是less,也就是说用迭代器迭代的时候是从小到大排序的。

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 ;
}
 
相关文章
|
7天前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
52 1
|
3月前
|
存储 缓存 JavaScript
Set和Map有什么区别?
Set和Map有什么区别?
282 1
|
4月前
|
存储 JavaScript 前端开发
for...of循环在遍历Set和Map时的注意事项有哪些?
for...of循环在遍历Set和Map时的注意事项有哪些?
271 121
|
4月前
|
存储 C++ 容器
unordered_set、unordered_multiset、unordered_map、unordered_multimap的介绍及使用
unordered_set是不按特定顺序存储键值的关联式容器,其允许通过键值快速的索引到对应的元素。在unordered_set中,元素的值同时也是唯一地标识它的key。在内部,unordered_set中的元素没有按照任何特定的顺序排序,为了能在常数范围内找到指定的key,unordered_set将相同哈希值的键值放在相同的桶中。unordered_set容器通过key访问单个元素要比set快,但它通常在遍历元素子集的范围迭代方面效率较低。它的迭代器至少是前向迭代器。前向迭代器的特性。
196 0
|
4月前
|
编译器 C++ 容器
用一棵红黑树同时封装出map和set
再完成上面的代码后,我们的底层代码已经完成了,这时候已经是一个底层STL的红黑树了,已经已符合库里面的要求了,这时候我们是需要给他穿上对应的“衣服”,比如穿上set的“衣服”,那么这个穿上set的“衣服”,那么他就符合库里面set的要求了,同样map一样,这时候我们就需要实现set与map了。因此,上层容器map需要向底层红黑树提供一个仿函数,用于获取T当中的键值Key,这样一来,当底层红黑树当中需要比较两个结点的键值时,就可以通过这个仿函数来获取T当中的键值了。我们就可以使用仿函数了。
52 0
|
4月前
|
存储 编译器 容器
set、map、multiset、multimap的介绍及使用以及区别,注意事项
set是按照一定次序存储元素的容器,使用set的迭代器遍历set中的元素,可以得到有序序列。set当中存储元素的value都是唯一的,不可以重复,因此可以使用set进行去重。set默认是升序的,但是其内部默认不是按照大于比较,而是按照小于比较。set中的元素不能被修改,因为set在底层是用二叉搜索树来实现的,若是对二叉搜索树当中某个结点的值进行了修改,那么这棵树将不再是二叉搜索树。
208 0
|
4月前
|
安全 Java 数据库连接
让我们讲解一下 Map 集合遍历的方式
我是小假 期待与你的下一次相遇 ~
147 43
使用 entrySet 遍历 Map 类集合 KV
使用 entrySet 遍历 Map 类集合 KV
|
存储 前端开发 API
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
该文章详细介绍了ES6中Set和Map数据结构的特性和使用方法,并探讨了它们在前端开发中的具体应用,包括如何利用这些数据结构来解决常见的编程问题。
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用

热门文章

最新文章