13-STL-二分查找
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的形式将两者都返回。
- #include <algorithm>
- #include <iostream>
- #include <iterator>
- #include <vector>
- using namespace std;
- int main()
- {
- vector<int> v;
- vector<int>::iterator iter;
- pair<vector<int>::iterator, vector<int>::iterator> vecpair;
- for(int i = 1; i<= 20; i++) {
- v.push_back(i%6);
- }
- sort(v.begin(), v.end());
- cout << "array: " << endl << " ";
- copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
- cout << endl << endl;
- /* lower_bound */
- cout << "lower_bound function, value = 3: " << endl;
- iter = lower_bound(v.begin(), v.end(), 3);
- cout << " [first, iter] = ";
- copy(v.begin(), iter, ostream_iterator<int>(cout, " "));
- cout << endl;
- cout << " [iter, last] = ";
- copy(iter, v.end(), ostream_iterator<int>(cout, " "));
- cout << endl << endl;
- /* upper_bound */
- cout << "upper_bound function, value = 3: " << endl;
- iter = upper_bound(v.begin(), v.end(), 3);
- cout << " [first, iter] = ";
- copy(v.begin(), iter, ostream_iterator<int>(cout, " "));
- cout << endl;
- cout << " [iter, last] = ";
- copy(iter, v.end(), ostream_iterator<int>(cout, " "));
- cout << endl << endl;
- /* equal_range */
- cout << "euqual_range function value = 3: " << endl;
- vecpair = equal_range(v.begin(), v.end(), 3);
- cout << " [vecpair->first, vecpair->second] = ";
- copy(vecpair.first, vecpair.second, ostream_iterator<int>(cout, " "));
- cout << endl << endl;
- /* binary_search */
- cout << "binary_search function value = 3: " << endl;
- cout << "3 is " << (binary_search(v.begin(), v.end(), 3) ? "": "not ") << " in array" << endl;
- cout << endl;
- /* binary_search */
- cout << "binary_search function value = 6: " << endl;
- cout << "6 is " << (binary_search(v.begin(), v.end(), 6) ? "": "not ") << " in array" << endl;
- cout << endl;
- }
- ./bsearch
- array:
- 0 0 0 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 5
- lower_bound function, value = 3:
- [first, iter] = 0 0 0 1 1 1 1 2 2 2 2
- [iter, last] = 3 3 3 4 4 4 5 5 5
- upper_bound function, value = 3:
- [first, iter] = 0 0 0 1 1 1 1 2 2 2 2 3 3 3
- [iter, last] = 4 4 4 5 5 5
- euqual_range function value = 3:
- [vecpair->first, vecpair->second] = 3 3 3
- binary_search function value = 3:
- 3 is in array
- binary_search function value = 6:
- 6 is not in array
13-STL-二分查找的更多相关文章
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...
- STL二分查找函数的应用
应用二分查找的条件必须是数组有序! 其中二分查找函数有三个binary_serch,upper_bound,lower_bound 测试数组 int n1[]={1,2,2,3,3,4,5}; int ...
- C++ STL 二分查找
转载自 https://www.cnblogs.com/Tang-tangt/p/9291018.html 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound ...
- SDUT2157——Greatest Number(STL二分查找)
Greatest Number 题目描述Saya likes math, because she think math can make her cleverer.One day, Kudo invi ...
- STL 二分查找
实现源码:https://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 1.在一个递增的数组(或vector)中查找元素属于[ s , ...
- 二分查找-python
约12年年底的时候,接触了python不到半年的样子,入门是直接实现GUI测试case的.今天面试地平线机器人,发现忘得差不多了- -. 当时的问题是这样的 写一个二分查找是实现,我好像不记得二分查找 ...
- C++STL标准库学习笔记(二)二分查找
二.STL中的二分查找算法 1.binary_search 2.lower_bound 3.upper_bound 记得#include<algorithm>! 前言: 在这个笔记中,我把 ...
- 【转】STL之二分查找 (Binary search in STL)
Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...
- STL之二分查找 (Binary search in STL)
STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound ...
- pearl(二分查找,stl)
最近大概把有关二分的题目都看了一遍... 嗯..这题是二分查找...二分查找的代码都类似,所以打起来会水很多 但是刚开始打二分还是很容易写挂..所以依旧需要注意 题2 天堂的珍珠 [题目描述] 我有很 ...
随机推荐
- 前端导出Excel
1.首先,需要下载一个叫better-xlsx,的插件,以yarn 为例 ' yarn add better-xlsx --save '下载相关依赖包( npm 方式 ' npm i better-x ...
- 1132. Cut Integer (20)
Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long int ...
- Windows 系统定时自动重启
1.创建新文本并输入 shutdown -r -t 0 保存成.bat文件 2.创建系统任务计划 2.1 在开始中打开[任务计划程序] 2.2 新建创建任务计划目录 2.3 在新目录下新建任务计划即可 ...
- Redis事件库源码分析
由于老大在新项目中使用redis的事件库代替了libevent,我也趁着机会读了一遍redis的事件库代码,第一次读到“优美,让人愉快”的代码,加之用xmind制作的类图非常帅,所以留文纪念. Red ...
- FastAdmin CMS 插件相关文章收集(2018-08-16)
FastAdmin CMS 插件相关文章收集(2018-08-16) CMS内容管理系统(含小程序) 介绍 https://www.fastadmin.net/store/cms.html CMS内容 ...
- android多渠道打包牛B工具
http://www.orchidshell.com/ 兰贝壳儿:一个Eclipse插件,为Android开发提供了多渠道打包功能和一些工具类.
- show-overflow-tooltip 宽度设置
设置样式:不能放在scoped中 <style lang="scss"> .el-tooltip__popper{max-width:80%}</style> ...
- svn配置及基本使用
svn软件下载地址http://subversion.apache.org/packages.html在安装TortoiseSVN时安装客户端和服务端 下以svn在windows下使用为例,linux ...
- android httpclient 设置超时
3.X是这样的 HttpClient httpClient=new DefaultHttpClient();4.3是这样的CloseableHttpClient httpClient = HttpCl ...
- 杂项-EMS:CRM
ylbtech-杂项-EMS:CRM CMS是"Content Management System"的缩写,意为"内容管理系统". 内容管理系统是企业信息化建设 ...