二分查找的函数有 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. scrapy爬虫系列之三--爬取图片保存到本地

    功能点:如何爬取图片,并保存到本地 爬取网站:斗鱼主播 完整代码:https://files.cnblogs.com/files/bookwed/Douyu.zip 主要代码: douyu.py im ...

  2. TA-Lib中文文档(二):talib安装

    安装 使用pip安装 PyPI: $ pip install TA-Lib Or checkout the sources and run setup.py yourself: $ python se ...

  3. 转!!java 堆栈内存 对象实例等查看

    https://blog.csdn.net/fenglibing/article/details/6411999

  4. T-SQL练习题

    转自:http://www.cnblogs.com/jenrrychen/p/5348546.html 1 - 3 题: 数据表结构: OrderID ProductID OrderDate  Sal ...

  5. Teigha.net读写dwg文件显示

    官网:http://www.opendesign.com/ http://www.cnblogs.com/zhanglibo0626/archive/2011/11/04/2236238.html 下 ...

  6. mysql 出现的错误

    1:创建函数时提示:This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA ,因为当开启log-bin时,函数必须有参数, ...

  7. [LeetCode]83. Remove Duplicates from Sorted List(排序链表去重)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  8. 120. Triangle(动态规划 三角形最小路径 难 想)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  9. Mysql索引结构与索引原理

    Mysql索引主要包括四种,Btree索引.Hash索引.full-text全文索引.R-tree索引,因为作为一名PHP开发者,并不是专业的DBA,在这里只需要了解第一种开发相关的BTree索引. ...

  10. 再论Splay

    联赛前为了填知识点,简单学了一下比较常用的高级数据结构,都没有太深入的理解,于是现在重新搞一遍. 其实有了set和multiset,那么我们就没有再手写平衡树的必要了,所以treap的应用就相对于Sp ...