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. BZOJ - 5427:最长上升子序列 (二分&思维)

    现在给你一个长度为n的整数序列,其中有一些数已经模糊不清了,现在请你任意确定这些整数的值, 使得最长上升子序列最长.(为何最长呢?因为hxy向来对自己的rp很有信心)   Input 第一行一个正整数 ...

  2. 组件与.NET互操作

    组件 1.何谓组件技术? 组件技术就是利用某种编程手段,将一些人们所关心的,但又不便于让最终用户去直接操作的细节进行了封装,同时对各种业务逻辑规则进行了实现,用于处理用户的内部操作细节,甚至于将安全机 ...

  3. PowerDesigner导出word表结构

    一.wordTemplate.rtp下载 首先下载wordTemplate.rtp,将该文件放在一下路径下 C:\Program Files (x86)\Sybase\PowerDesigner 16 ...

  4. bzoj 1499 [NOI2005]瑰丽华尔兹——单调队列优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1499 简单的单调队列优化dp.(然而当时却WA得不行.今天总算填了坑) 注意滚动数组赋初值应 ...

  5. 洛谷 2831 (NOIp2016) 愤怒的小鸟——仅+1所以bfs优化

    题目:https://www.luogu.org/problemnew/show/P2831 状压dp.跑得很慢.(n^2*2^n) 注意只打一只猪的情况. #include<iostream& ...

  6. android中HttpClient的应用(POST方法)

    首先在http://hc.apache.org/downloads.cgi下载HttpClient包 直接看代码 import android.os.Handler; import android.o ...

  7. ORACLE常识

    1. ORACLE中查看表中的外键来源于哪些表 select cl.table_name from user_cons_columns cl left join user_constraints c ...

  8. Object-C 多线程中锁的使用-NSLock

    在多线程的编程环境中,锁的使用必不可少! 于是,今天来总结一下为共享资源加锁的操作方法.   一.使用synchronized方式   //线程1 dispatch_async(dispatch_ge ...

  9. Fiddler2 模拟文件上传

    最近遇到一个需求,需要上传音频文件, 服务端使用webService 通过spring3 进行文件上传.代码完成后使用 html 通过post 方式请求接口成功了,但不知道如何使用Fiddler2工具 ...

  10. ByteBuf 类——Netty 的数据容器

    1.堆缓冲区 2.直接缓冲区 3.复合缓冲区 —CompositeByteBuf——实现了这个模式,它提供了一 个将多个缓冲区表示为单个合并缓冲区的虚拟表示 适用于 JDK 所使用的一种称为分散/收集 ...