STL中提供-二分查找算法(binary_search lower_bound upper_bound equal_range

 

STL包含四种不同的二分查找算法,binary_search    lower_bound  upper_bound   equal_range.他们作用的range是已sorted。

binary_search试图在已排序的[first, last)中寻找元素value。如果[first, last)内有等价于value的元素,它会返回true,否则返回false,它不返回查找位置。

lower_bound它试图在已排序的[first,last)中寻找元素value。如果[first, last)具有等价于value的元素,lower_bound返回一个iterator指向其中第一个元素。如果没有这样的元素存在,它便返回假设这样的元素存在的话,会出现的位置。即指向第一个不小于value的元素。如果value大于[first, last)的任何一个元素,则返回last。

upper_bound它试图在已排序的[first,last)中寻找value,返回可安插value的最后一个合适的位置。如果value存在,lower_bound 返回的是指向该元素的iterator。相较之下upper_bound并不这么做,它返回value可被安插的最后一个合适位置。如果value存在,那么它返回的iterator将指向value的下一个位置,而非value自身。

equal_range的返回值本质上结合了lower_bound和upper_bound两者的返回值。其返回值是一对iterator i 和 j , 其中i是value可安插的第一个位置,j则是value可安插的最后一个位置。可以推演出:[i,j)中的每个元素都等价于value,而且[i, j)是[first, last)之中符合上述性质的一个最大子区间。  算法lower_bound返回该range的第一个iterator, 算法upper_bound返回该range的past-the-end iterator,算法equal_range则是以pair的形式将两者都返回。

 
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <vector>
  5. using namespace std;
  6. int main()
  7. {
  8. vector<int> v;
  9. vector<int>::iterator iter;
  10. pair<vector<int>::iterator, vector<int>::iterator> vecpair;
  11. for(int i = 1; i<= 20; i++) {
  12. v.push_back(i%6);
  13. }
  14. sort(v.begin(), v.end());
  15. cout << "array: " << endl << "  ";
  16. copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
  17. cout << endl << endl;
  18. /*  lower_bound */
  19. cout << "lower_bound function, value = 3: " << endl;
  20. iter = lower_bound(v.begin(), v.end(), 3);
  21. cout << "  [first, iter] = ";
  22. copy(v.begin(), iter, ostream_iterator<int>(cout, " "));
  23. cout << endl;
  24. cout << "  [iter, last] = ";
  25. copy(iter, v.end(), ostream_iterator<int>(cout, " "));
  26. cout << endl << endl;
  27. /*  upper_bound */
  28. cout << "upper_bound function, value = 3: " << endl;
  29. iter = upper_bound(v.begin(), v.end(), 3);
  30. cout << "  [first, iter] = ";
  31. copy(v.begin(), iter, ostream_iterator<int>(cout, " "));
  32. cout << endl;
  33. cout << "  [iter, last] = ";
  34. copy(iter, v.end(), ostream_iterator<int>(cout, " "));
  35. cout << endl << endl;
  36. /*  equal_range */
  37. cout << "euqual_range function value = 3: " << endl;
  38. vecpair = equal_range(v.begin(), v.end(), 3);
  39. cout << " [vecpair->first, vecpair->second] = ";
  40. copy(vecpair.first, vecpair.second, ostream_iterator<int>(cout, " "));
  41. cout << endl << endl;
  42. /*  binary_search */
  43. cout << "binary_search function value = 3: " << endl;
  44. cout << "3 is " << (binary_search(v.begin(), v.end(), 3) ? "": "not ") << " in array" << endl;
  45. cout << endl;
  46. /*  binary_search */
  47. cout << "binary_search function value = 6: " << endl;
  48. cout << "6 is " << (binary_search(v.begin(), v.end(), 6) ? "": "not ") << " in array" << endl;
  49. cout << endl;
  50. }
  51. ./bsearch
  52. array:
  53. 0 0 0 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 5
  54. lower_bound function, value = 3:
  55. [first, iter] = 0 0 0 1 1 1 1 2 2 2 2
  56. [iter, last] = 3 3 3 4 4 4 5 5 5
  57. upper_bound function, value = 3:
  58. [first, iter] = 0 0 0 1 1 1 1 2 2 2 2 3 3 3
  59. [iter, last] = 4 4 4 5 5 5
  60. euqual_range function value = 3:
  61. [vecpair->first, vecpair->second] = 3 3 3
  62. binary_search function value = 3:
  63. 3 is  in array
  64. binary_search function value = 6:
  65. 6 is not  in array

13-STL-二分查找的更多相关文章

  1. STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())

    一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search  -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...

  2. STL二分查找函数的应用

    应用二分查找的条件必须是数组有序! 其中二分查找函数有三个binary_serch,upper_bound,lower_bound 测试数组 int n1[]={1,2,2,3,3,4,5}; int ...

  3. C++ STL 二分查找

    转载自 https://www.cnblogs.com/Tang-tangt/p/9291018.html 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound ...

  4. SDUT2157——Greatest Number(STL二分查找)

    Greatest Number 题目描述Saya likes math, because she think math can make her cleverer.One day, Kudo invi ...

  5. STL 二分查找

    实现源码:https://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 1.在一个递增的数组(或vector)中查找元素属于[ s , ...

  6. 二分查找-python

    约12年年底的时候,接触了python不到半年的样子,入门是直接实现GUI测试case的.今天面试地平线机器人,发现忘得差不多了- -. 当时的问题是这样的 写一个二分查找是实现,我好像不记得二分查找 ...

  7. C++STL标准库学习笔记(二)二分查找

    二.STL中的二分查找算法 1.binary_search 2.lower_bound 3.upper_bound 记得#include<algorithm>! 前言: 在这个笔记中,我把 ...

  8. 【转】STL之二分查找 (Binary search in STL)

    Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...

  9. STL之二分查找 (Binary search in STL)

    STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound ...

  10. pearl(二分查找,stl)

    最近大概把有关二分的题目都看了一遍... 嗯..这题是二分查找...二分查找的代码都类似,所以打起来会水很多 但是刚开始打二分还是很容易写挂..所以依旧需要注意 题2 天堂的珍珠 [题目描述] 我有很 ...

随机推荐

  1. header("Location:http://www.baidu.com");

    php 中的跳转函数 header("Location:http://www.baidu.com"); 但是一定要放在文件的开头   不允许有任何输出. 否则在之前添加  ob_s ...

  2. httpwebrequest webrequest webresponse 总结

    http://blog.csdn.net/flymorn/article/details/6769722 使用,总结,深入,全通,指正

  3. vue指令总结

    new  Vue({          el: "#box",            // element(元素) 当前作用域          data:{ msg:" ...

  4. mysql5.6.11安装

    下面详细介绍5.6版本MySQL的下载.安装及配置过程. 图1.1 MySQL5.6 目前针对不同用户,MySQL提供了2个不同的版本: Ø         MySQL Community Serve ...

  5. RK3288 dts文件中背光配置参数

    backlight { // 设备名(用于匹配) compatible = "pwm-backlight"; // pwm编号 通道 频率(ns) pwms = <& ...

  6. mysql命令 show slave status\G;命令输出详解

    show slave status\G; 命令输出详解 mysql> show slave status\G; *************************** . row ******* ...

  7. Python3中的http.client模块

    http 模块简介 Python3 中的 http 包中含有几个用来开发 HTTP 协议的模块. http.client 是一个底层的 HTTP 协议客户端,被更高层的 urllib.request ...

  8. DHCP(一)

    DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络服务供应商自动分配IP ...

  9. php排序集合

    如果你已经使用了一段时间PHP的话,那么,你应该已经对它的数组比较熟悉了——这种数据结构允许你在单个变量中存储多个值,并且可以把它们作为一个集合进行操作. 经常,开发人员发现在PHP中使用这种数据结构 ...

  10. Redis数据清除问题

    Redis中数据清除可以分为两种方式 手动清除:指定要清除的key,通过delete命令即可清除 自动清除:使用Redis提供的数据过期策略 Redis数据过期策略      redis提供了非常灵活 ...