博客
关于我
c++之greater和less在stl中运用
阅读量:195 次
发布时间:2019-02-28

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

greater< type >()和less< type >()是functional下的两个仿函数,都重载了操作符,它们的源码如下:

greater

/// One of the @link comparison_functors comparison functors@endlink.  template
struct greater : public binary_function<_Tp, _Tp, bool> { bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; } };

less

/// One of the @link comparison_functors comparison functors@endlink.  template
struct less : public binary_function<_Tp, _Tp, bool> { bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; } };

可见greater是从大到小排,less是从小到大排。

stl中sort(begin, end),lower_bound( begin,end,num)和upper_bound( begin,end,num)默认的是从小到大排,如果要从大到小排,在最后的参数上写上“greater< int >()”或者写自定义比较函数:

bool cmp(const int& a,const int& b){   return a > b;}

greater< int>()表示内置类型从大到小排序,比如说原序列是1,2,4,7,15,34,在greater< int>()的表示下,1>2>4>7>15>34,

优先队列priority_queue< type >和上面的不一样,它的默认的取出顺序是从大到小的,相当于less,从小到大就要写成

priority_queue 
,greater
> q;

转载地址:http://qrki.baihongyu.com/

你可能感兴趣的文章
MySQL5.6的zip包安装教程
查看>>
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
Webpack 基本环境搭建
查看>>
mysql5.7 安装版 表不能输入汉字解决方案
查看>>
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>