STL中sort、priority_queue、map、set的自定义比较函数
来源:互联网 发布:c unity3d 仿真 编辑:程序博客网 时间:2024/05/22 00:46
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 ;}
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 ;}
#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 ;}
- STL中sort、priority_queue、map、set的自定义比较函数
- STL中sort、priority_queue、map、set的自定义比较函数
- STL set,map , priority_queue 及 lower_bound 的排序比较函数
- STL中set、map的比较函数
- map及set中自定义比较函数
- stl中map自定义比较函数
- stl中map自定义比较函数
- STL中比较常用的容器是vector,set和map,比较常用的算法有Sort等。
- STL中常用的vector,map,set,sort 用法
- STL中常用的vector,map,set,sort 用法
- STL中常用的vector,map,set,sort 用法
- STL中常用的vector,map,set,sort 用法 转
- STL中map和priority_queue的应用
- sort, priority_queue, set, map排序顺序
- C++ STL set自定义比较函数
- C++ STL set自定义比较函数
- map和set容器自定义比较函数
- STL:set中使用自定义比较操作
- php通过xpath访问xml文件
- TM框架-MySQL版本(实现动态开发和权限管理)-附下载地址
- xml DTD验证
- C/C++中函数参数传递详解
- 从阿里云os和Google之争看开源协议
- STL中sort、priority_queue、map、set的自定义比较函数
- 『转』Struts2总结(一)
- 异常机制
- linux 新建用户、用户组及用户权限
- 红黑树(Red Black Tree)
- joomla模板首页无内容时自动隐藏主体内容区域
- zend studio 出现乱码问题总结
- 测试从零开始(一)测试基本概念
- oracle 里的函数调用问题和触发器,函数包的问题