转载于:http://blog.csdn.net/tjpuacm/article/details/26389441

不加比较函数的情况:

  1. int a[]={0,1,2,2,3};
  2. printf("%d\n",lower_bound(a,a+5,2,cmp)-a);
  3. 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号位置

加了比较函数:

  1. bool cmp(int a,int b)
  2. {
  3. return a<b;
  4. }
  5. int main()
  6. {
  7. int a[]={0,1,2,2,3};
  8. printf("%d\n",lower_bound(a,a+5,2,cmp)-a);
  9. printf("%d\n",upper_bound(a,a+5,2,cmp)-a);
  10. return 0 ;
  11. }

结果仍然是2 4 ,可以得出一个结论,cmp里函数应该写的是小于运算的比较

如果加上了等号,lower和upper两个函数功能就刚好反过来了:

  1. bool cmp(int a,int b)
  2. {
  3. return a<=b;
  4. }
  5. int main()
  6. {
  7. int a[]={0,1,2,2,3};
  8. printf("%d\n",lower_bound(a,a+5,2,cmp)-a);
  9. printf("%d\n",upper_bound(a,a+5,2,cmp)-a);
  10. return 0 ;
  11. }

结果是4 2

C/C++-STL中lower_bound与upper_bound的用法以及cmp函数的更多相关文章

  1. STL中mem_fun和mem_fun_ref的用法

    例如:假设有如下的代码: class Employee { public: int DoSomething(){} } std::vector<Employee> Emps; 假设我们要调 ...

  2. STL中find和sort的用法总结

    STL算法 STL 提供能在各种容器中通用的算法(大约有70种),如插入.删除.查找.排序等. 许多算法操作的是容器上的一个区间(也可以是整个容器),因此需要两个参数,一个是区间起点元素的迭代器,另一 ...

  3. STL之lower_bound和upper_bound

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

  4. C++ STL:lower_bound与upper_bound实现

    lower_bound lower_bound(begin, end, target)用来查找一个已排序的序列中[begin, end)第一个大于等于target的元素index.数组A如下: val ...

  5. 【转】STL中mem_fun和mem_fun_ref的用法及区别

    原文:http://www.cppblog.com/mysileng/archive/2012/12/25/196615.html 引子: 怎么对容器中的所有对象都进行同一个操作?我们可能首先想到的是 ...

  6. STL中map,set的基本用法示例

    本文主要是使用了STL中德map和set两个容器,使用了它们本身的一些功能函数(包括迭代器),介绍了它们的基本使用方式,是一个使用熟悉的过程. map的基本使用: #include "std ...

  7. 徒手实现lower_bound和upper_bound

    STL中lower_bound和upper_bound的使用方法:STL 二分查找 lower_bound: ; ; //初始化 l ,为第一个合法地址 ; //初始化 r , 地址的结束地址 int ...

  8. STL之std::set、std::map的lower_bound和upper_bound函数使用说明

    由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...

  9. STL中的stack(堆栈)

    转载:http://blog.csdn.net/morewindows/article/details/6950881 栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(F ...

随机推荐

  1. IOS 项目的瘦身工具

    http://maniacdev.com/2014/01/tool-a-ruby-gem-allowing-you-to-quickly-find-and-remove-unused-imports- ...

  2. Traefik Kubernetes 初试

    traefik 是一个前端负载均衡器,对于微服务架构尤其是 kubernetes 等编排工具具有良好的支持:同 nginx 等相比,traefik 能够自动感知后端容器变化,从而实现自动服务发现:今天 ...

  3. [svc]为何linux ext4文件系统目录默认大小是4k?

    linux ext4普通盘为什么目录大小是4k? Why does every directory have a size 4096 bytes (4 K)? To understand this, ...

  4. 使用FiddlerCore来截取替换Http请求中的网页内容

    做过测试的应该都知道Fiddler,它可以很方便截取Internet上的网页替换成本地的,或者修改其中的一部分内容后呈现.简单地说就是可能监测所有HTTP连接,设置断点,胡乱修改.是测试调试的一件利器 ...

  5. [Windows Azure] How to use the Table Storage Service

    How to use the Table Storage Service version 1.7 version 2.0 This guide will show you how to perform ...

  6. javascript基础拾遗(九)

    1.class关键字 ES6引入了新的class关键字编写对象 function Language(name){ this.name = name this.score = 8.0 } Languag ...

  7. (转)Go和HTTPS

    转自:http://studygolang.com/articles/2946 Go和HTTPS  2015-04-30   bigwhite  阅读 5688 次   4 人喜欢  3 条评论  收 ...

  8. kafka配置参数详解【收藏】

    3.1      Broker  Configs 基本配置如下: -broker.id -log.dirs -zookeeper.connect Topic-level配置以及其默认值将在下面讨论. ...

  9. Mybatis的mapper注册

    既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要定义 SQL 映射语句了.但是首先我们需要告诉 MyBatis 到哪里去找到这些语句. Java 在自动查找这方面没有提供一个很好的方法 ...

  10. js弹出层的插件

    1.jquery.fancybox.pack.js 2.artdialog 3.