C/C++-STL中lower_bound与upper_bound的用法以及cmp函数
转载于:http://blog.csdn.net/tjpuacm/article/details/26389441
不加比较函数的情况:
- int a[]={0,1,2,2,3};
- printf("%d\n",lower_bound(a,a+5,2,cmp)-a);
- printf("%d\n",upper_bound(a,a+5,2,cmp)-a);
结果:2 4
lower的意义是对于给定的已经排好序的a,key最早能插入到那个位置
0 1 | 2 2 3 所以2最早插入到2号位置
upper的意义是对于给定的已经排好序的a,key最晚能插入到那个位置
0 1 2 2 | 3 所以2最晚插入到4号位置
加了比较函数:
- bool cmp(int a,int b)
- {
- return a<b;
- }
- int main()
- {
- int a[]={0,1,2,2,3};
- printf("%d\n",lower_bound(a,a+5,2,cmp)-a);
- printf("%d\n",upper_bound(a,a+5,2,cmp)-a);
- return 0 ;
- }
结果仍然是2 4 ,可以得出一个结论,cmp里函数应该写的是小于运算的比较
如果加上了等号,lower和upper两个函数功能就刚好反过来了:
- bool cmp(int a,int b)
- {
- return a<=b;
- }
- int main()
- {
- int a[]={0,1,2,2,3};
- printf("%d\n",lower_bound(a,a+5,2,cmp)-a);
- printf("%d\n",upper_bound(a,a+5,2,cmp)-a);
- return 0 ;
- }
结果是4 2
C/C++-STL中lower_bound与upper_bound的用法以及cmp函数的更多相关文章
- STL中mem_fun和mem_fun_ref的用法
例如:假设有如下的代码: class Employee { public: int DoSomething(){} } std::vector<Employee> Emps; 假设我们要调 ...
- STL中find和sort的用法总结
STL算法 STL 提供能在各种容器中通用的算法(大约有70种),如插入.删除.查找.排序等. 许多算法操作的是容器上的一个区间(也可以是整个容器),因此需要两个参数,一个是区间起点元素的迭代器,另一 ...
- STL之lower_bound和upper_bound
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...
- C++ STL:lower_bound与upper_bound实现
lower_bound lower_bound(begin, end, target)用来查找一个已排序的序列中[begin, end)第一个大于等于target的元素index.数组A如下: val ...
- 【转】STL中mem_fun和mem_fun_ref的用法及区别
原文:http://www.cppblog.com/mysileng/archive/2012/12/25/196615.html 引子: 怎么对容器中的所有对象都进行同一个操作?我们可能首先想到的是 ...
- STL中map,set的基本用法示例
本文主要是使用了STL中德map和set两个容器,使用了它们本身的一些功能函数(包括迭代器),介绍了它们的基本使用方式,是一个使用熟悉的过程. map的基本使用: #include "std ...
- 徒手实现lower_bound和upper_bound
STL中lower_bound和upper_bound的使用方法:STL 二分查找 lower_bound: ; ; //初始化 l ,为第一个合法地址 ; //初始化 r , 地址的结束地址 int ...
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...
- STL中的stack(堆栈)
转载:http://blog.csdn.net/morewindows/article/details/6950881 栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(F ...
随机推荐
- Linux中用户及用户组
Linux用户只有两个等级:root及非root.Linux中还有一部分用户,如:apache.mysql.nobody.ftp等,这些也都是非root用户,即普通用户.Linux的权限实际是上不同用 ...
- POJ 1636 Prison rearrangement DFS+0/1背包
题目链接: id=1636">POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS Memor ...
- HTTP模块SuperAgent
superagent它是一个强大并且可读性很好的轻量级ajaxAPI,是一个关于HTTP方面的一个库,而且它可以将链式写法玩的出神入化. var superagent = require('super ...
- RDD PAPER
https://cs.stanford.edu/~matei/ https://www2.eecs.berkeley.edu/Pubs/TechRpts/2014/EECS-2014-12.pdf h ...
- Python args **kwargs作用
python当函数的参数不确定时,可以使用*args和**kwargs,*args用于捕获所有no keyword参数,它是一个tuple.**kwargs捕获所有keyword参数,它是一个dict ...
- 非nodejs方式的vue.js的使用
1.node环境 详细见我之前的文章,node的安装 2.git环境 git bash命令窗,支持bash命令,cmd不支持bash命令 3.cnpm安装 cnpm是针对国内使用npm网络慢的而使用的 ...
- (原)docker安装
2018直接看这里: https://docs.docker.com/cs-engine/1.12/#install-on-ubuntu-1404-lts-or-1604-lts 以下为原文: 网上的 ...
- 【Mysql】Fedora下 Mysql 安装及配置
1.安装 Mysql Server # yum install mysql mysql-server 可以到mysql官网去下载,我下载的是通用版本.你需要下载下面四个文件就可以了. mysql-cl ...
- System.in的用法
方法1 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));Scanner scanner=new Sca ...
- ostream_iterator的可能实现
当我们要输出一个容器的内容时,可以使用std::copy函数,如下: vector <string> myvector; std::copy(myvector.begin(), myvec ...