博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
STL中sort、priority_queue、map、set的自定义比较函数
阅读量:4698 次
发布时间:2019-06-09

本文共 3339 字,大约阅读时间需要 11 分钟。

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

1、sort

#include 
#include
#include
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
()排序 sort( array , array+6 , greater
() ) ; //从大到小排序 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 
#include
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
q ; //优先队列默认是less,大顶堆 ; //priority_queue
,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
#include
#include
#include
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
,bool (*)(int,int)> ipq(ia,ia+9,cmp); //默认参数要从左到右开始指定 cout<<"size=" <
<

3、map

 

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

 

4、set

#include 
#include
#include
#include
using namespace std;struct cmp{ bool operator()( const int& a , const int& b )const{ return a < b ; //从小到大 }} ;int main(){ //set
s ; set
s ; for(int i=0;i<10;i++) s.insert( rand() ) ; set
::iterator it = s.begin() ; for( ; it!=s.end();it++) printf("%d\n",*it); return 0 ;} 令一种比较函数的声明方式,在构造函数中初始化:
#include 
#include
#include
#include
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
s ; set
s(mycmp) ; for(int i=0;i<10;i++) s.insert( rand() ) ; set
::iterator it = s.begin() ; for( ; it!=s.end();it++) printf("%d\n",*it); return 0 ;}
 

转载于:https://www.cnblogs.com/wuchanming/p/4409382.html

你可能感兴趣的文章
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
arrow:让Python的日期与时间变的更好
查看>>
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
postgressql数据库中limit offset使用
查看>>
测试思想-集成测试 关于接口测试 Part 2
查看>>
php生成器使用总结
查看>>
T-SQL中的indexof函数
查看>>
javascript基础之数组(Array)对象
查看>>
mysql DML DDL DCL
查看>>
RAMPS1.4 3d打印控制板接线与测试1
查看>>
python with语句中的变量有作用域吗?
查看>>
24@Servlet_day03
查看>>