二分查找的函数有 3 个:

1.lower_bound(起始地址,结束地址,要查找的数值) 返回的是数值 第一个 出现的位置。

2.upper_bound(起始地址,结束地址,要查找的数值) 返回的是数值 最后一个 出现的位置。

3.binary_search(起始地址,结束地址,要查找的数值)  返回的是是否存在这么一个数,是一个bool值。

举例:

数组 a(下标从1开始) : 1  2  3  3  3  4  5

查找 3 :

int position1 = lower_bound(a+,a+n,) - a; //position1 = 3
int position2 = upper_bound(a+,a+n,) - a; //position2 = 5 

需要注意的是:如果数组中没有找到所求元素,函数就会返回一个假想的插入位置。

int a[] = {,,,,,};
int position1 = lower_bound(a+,a+,) - a;
int position2 = upper_bound(a+,a+,) - a;
cout << position1 << endl;
cout << position2 << endl;
// position1 = position2 = 3

还有二分查找的首要条件是数列有序!

C++ lower_bound 和upper_bound的更多相关文章

  1. STL源码学习----lower_bound和upper_bound算法

    转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...

  2. [STL] lower_bound和upper_bound

    STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...

  3. vector的插入、lower_bound、upper_bound、equal_range实例

    对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...

  4. STL中的lower_bound和upper_bound的理解

    STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...

  5. STL 源码分析《5》---- lower_bound and upper_bound 详解

    在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...

  6. lower_bound和upper_bound算法

    参考:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html ForwardIter lower_bound(ForwardIte ...

  7. lower_bound 和 upper_bound

    Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...

  8. STL源码学习----lower_bound和upper_bound算法[转]

    STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...

  9. [转] STL源码学习----lower_bound和upper_bound算法

    http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...

  10. STL algorithm算法lower_bound和upper_bound(31)

    lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...

随机推荐

  1. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect.

    Spring Framework Overview https://www.tutorialspoint.com/spring/spring_overview.htm Aspect Oriented ...

  2. Nginx高级玩法

    1. Nginx获取自定义消息头 .nginx是支持读取非nginx标准的用户自定义header的,但是需要在http或者server下开启header的下划线支持: underscores_in_h ...

  3. 云备份厂商Rubrik再获2.61亿美元融资,估值高达33亿美元 转自中国存储网

    数据管理初创公司Rubrik在Bain Capital Ventures领导的最新一轮融资中筹集了2.61亿美元,估值为33亿美元. 现有的利益相关者 - Lightspeed Venture Par ...

  4. java 调用静态方法和构造函数和静态块执行的先后顺序

    构造方法是只有你在new对象的时候才会执行,静态语句块和静态方法在类加载到内存的时候就已经执行了,另外,静态语句块只能给静态变量赋值,里面不能出现方法,同样,静态方法里面也不能出现静态语句块 追问: ...

  5. api收录

    ip地址查询api http://ip.taobao.com/service/getIpInfo.php?ip= 如: http://ip.taobao.com/service/getIpInfo.p ...

  6. [Axiom 3D]3.SceneManager场景管理器

    首先看看Axiom.Core命名空间下public abstract class SceneManager : DisposableObject A SceneManager organizes th ...

  7. mysql创建和删除表

    创建表 简单的方式 CREATE TABLE person ( ), name ), birthday DATE ); 或者是 CREATE TABLE IF NOT EXISTS person ( ...

  8. SaltStack系列(四)之实例编写

    前面已经介绍的够多了,这里来让我们写一些完整的实例来梳理一下. 强调一下,sls文件的抒写格式都是"-"后面跟一个空格,然后后面跟参数: 然后后面再跟一个空格,然后是要填写的值.但 ...

  9. (转) SpringBoot非官方教程 | 第十一篇:springboot集成swagger2,构建优雅的Restful API

    swagger,中文“拽”的意思.它是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试.另外swagger很容易构建restful风格的api,简单优雅 ...

  10. PAT 1044 Shopping in Mars[二分][难]

    1044 Shopping in Mars(25 分) Shopping in Mars is quite a different experience. The Mars people pay by ...