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 ...
随机推荐
- es6Promise及小程序Promise用法
本文主要说一下Promise,Prepending(进行时),Resolve(成功了),Reject(失败了),then在小程序中的实际应用 关于promise的介绍什么的就不说了网上一搜一大堆,这里 ...
- idea自动编译
idea修改后台代码自动编译 On 'update' action = Update classes and resources On frame deactivation = Update clas ...
- oozie 常用命令
1.验证wokflow.xmloozie validate /appcom/apps/hduser0401/mbl_webtrends/workflow.xml 2.提交作业,作业进入PREP状态 o ...
- UIButton 标题靠右
_classBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _classBtn.frame = CGRectMake( kDeviceWid ...
- jsp页面中的EL表达式不被解析的问题
原因:问题在web.xml配置文件上,web.xml声明部分一般分为如下版本的xsd, web-app_2_2.xsd web-app_2_3.xsd web-app_2_4.xsd web-app_ ...
- Android log 日志分析
一. Log 日志中 Bug 类型 程序异常强制关闭: Force Close ,Fatal 程序无响应: Application Not Response , ANR(应用无响应).一般是主线程超时 ...
- android:ellipsize="end" 失效或者 相关的Bug
其实这文章有点傻逼. 相关的问题 TextView android:ellipsize=“end”超出一个字符时不显示…的解决 http://www.pocketdigi.com/20140122/1 ...
- 牛津大学xDeepMind自然语言处理
http://study.163.com/course/introduction/1004336028.htm
- 未能为数据库 '*'中得对象'*'分配空间,因文件组'PRIMARY'已满
服务器使用mssqlserver2005,最近经常出现无法新增信息错误,查看日志,发现严重错误提示,内容大致为: 无法为数据库 'weixin_main' 中的对象 'dbo.wx_logs'.'PK ...
- Testng生成的测试报告乱码解决办法
Testng生成的测试报告乱码解决办法 2017-06-16 1 问题描述 乱码是程序编码不统一,比如Java源代码是utf-8,编译是gbk,这时会乱码. 代码如下: org.testng.Repo ...