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 天堂的珍珠 [题目描述] 我有很 ...
随机推荐
- 【ci框架学习】环境搭建
系统 -- Ubuntu 14.0(虚拟机linux 实体机Windows) 目标环境 -- lnmp 附加内容: 1.目录共享(方便代码编写) 2.使用secure crt终端软件连接(便于操作,不 ...
- Switch能否使用String做参数
在Java语言中Swith可以使用参数类型有:Only convertible int values, strings or enum variables are permitted 可以自动转换为整 ...
- String类的一些常规方法
String类 String类常用方法: ①length(): length() 长度 方法** 对比:数组.length 属性** 一般情况下,一个数字,一个字母,一个汉 ...
- imageView添加阴影和边框
注意:大量设置阴影会造成卡顿!!! 用上这句之后流畅度大大增加:imageV.layer.shouldRasterize = YES; 例: // 设置阴影 imageV.layer.shadowOf ...
- UVA136 Ugly Numbers
题意 PDF 分析 用堆和集合维护即可. 时间复杂度\(O(1500 \log n)\) 代码 #include<iostream> #include<cstdio> #inc ...
- fn project k8s 集成
具体部署还是比较简单的,以下为官方参考,只是有一个service type 为 loadBlancer 实际使用需要修改为NodePort Prerequisite 1: working Kuber ...
- Pycharm简单使用教程
转自https://blog.csdn.net/qq_40130759/article/details/79421242 1.下载pycharm pycharm是一种Python IDE,能够帮助我们 ...
- ZenCoding[Emmet]語法簡介【轉】
快速指南 下面是一些常用的Zen Coding功能,目前VS2013的Web Essentials插件已经支持. '#' 创建一个id特性 '.' 创建一个类特性 '[]' 创建一个自定义特性 '&g ...
- etcd使用经历
etcd是一个开源的.分布式的键值对数据存储系统,提供共享配置.服务的注册和发现.etcd与zookeeper相比算是轻量级系统,两者的一致性协议也一样,etcd的raft比zookeeper的pax ...
- Dobbo问题及解决方案:forbid-consumer
本地运行Dubbo经常出现以下情况: com.alibaba.dubbo.rpc.RpcException: Forbid consumer 10.0.53.69 access service com ...