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 ;
}
 
相关文章
|
5月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
356 1
|
8月前
|
存储 缓存 JavaScript
Set和Map有什么区别?
Set和Map有什么区别?
585 1
|
5月前
|
存储 算法 容器
set_map的实现+set/map加持秒杀高频算法题锻炼算法思维
`set`基于红黑树实现,支持有序存储、自动去重,增删查效率为O(logN)。通过仿函数可自定义排序规则,配合空间配置器灵活管理内存。不支持修改元素值,迭代器失效需注意。`multiset`允许重复元素。常用于去重、排序及查找场景。
|
9月前
|
存储 JavaScript 前端开发
for...of循环在遍历Set和Map时的注意事项有哪些?
for...of循环在遍历Set和Map时的注意事项有哪些?
432 121
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
329 2
|
9月前
|
存储 C++ 容器
unordered_set、unordered_multiset、unordered_map、unordered_multimap的介绍及使用
unordered_set是不按特定顺序存储键值的关联式容器,其允许通过键值快速的索引到对应的元素。在unordered_set中,元素的值同时也是唯一地标识它的key。在内部,unordered_set中的元素没有按照任何特定的顺序排序,为了能在常数范围内找到指定的key,unordered_set将相同哈希值的键值放在相同的桶中。unordered_set容器通过key访问单个元素要比set快,但它通常在遍历元素子集的范围迭代方面效率较低。它的迭代器至少是前向迭代器。前向迭代器的特性。
459 0
|
9月前
|
编译器 C++ 容器
用一棵红黑树同时封装出map和set
再完成上面的代码后,我们的底层代码已经完成了,这时候已经是一个底层STL的红黑树了,已经已符合库里面的要求了,这时候我们是需要给他穿上对应的“衣服”,比如穿上set的“衣服”,那么这个穿上set的“衣服”,那么他就符合库里面set的要求了,同样map一样,这时候我们就需要实现set与map了。因此,上层容器map需要向底层红黑树提供一个仿函数,用于获取T当中的键值Key,这样一来,当底层红黑树当中需要比较两个结点的键值时,就可以通过这个仿函数来获取T当中的键值了。我们就可以使用仿函数了。
136 0
|
9月前
|
存储 编译器 容器
set、map、multiset、multimap的介绍及使用以及区别,注意事项
set是按照一定次序存储元素的容器,使用set的迭代器遍历set中的元素,可以得到有序序列。set当中存储元素的value都是唯一的,不可以重复,因此可以使用set进行去重。set默认是升序的,但是其内部默认不是按照大于比较,而是按照小于比较。set中的元素不能被修改,因为set在底层是用二叉搜索树来实现的,若是对二叉搜索树当中某个结点的值进行了修改,那么这棵树将不再是二叉搜索树。
329 0
|
编译器 容器
哈希表模拟封装unordered_map和unordered_set
哈希表模拟封装unordered_map和unordered_set