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 天堂的珍珠 [题目描述] 我有很 ...
随机推荐
- php 设计数据库连接池
摘要 之前总是以脚本面向过程的方式写PHP代码,所以很大程度上来说,既不规范,也不安全,更不容易维护.为了代码的重用,准备写一套自己的工具库,这样的话,以后写项目的时候就可以很轻松的进行使用啦. 今天 ...
- Microsoft office2007免费版下载(安装 + 破解)
office2007官方下载 免费完整版是微软推出的办公软件,office2007使用方法很简单,解压软件之后,运行“setup.exe”之后按照提示点击下一步,输入产品秘钥,就可以正常安装了.Mic ...
- 关于AutoCommit
AutoCommit设置为true(大多数JDBCdrive的默认配置),则每次执行的SQL语句执行完成后都会落实到数据库中:如果想要在跨语句事务,则需要添加Begin Transiction,Com ...
- Voting and Shuffling to Optimize Atomic Operations
2iSome years ago I started work on my first CUDA implementation of the Multiparticle Collision Dynam ...
- 使用XV-11激光雷达做hector_slam
大家在学习ROS中不可避免需要使用激光雷达,高精地图.实时定位以及障碍物检测等多项技术,而这些技术都离不开光学雷达的支持,但是呢雷达这真是太贵了,大部分人是负担不起(实验室.研究所土豪可以略过),但是 ...
- cowboy的例子
大体参考的这里,非常感谢他的例子 开发的时候先下载好cowboy的库,放到~/.erlang里面 code:add_pathz("/Users/mmc/erlang/3rd_libs/cow ...
- sql中case when的简单使用
这是一个很多博客都引用的博客,作者未知,但是我第一次看到的就是这个,所以置顶这个吧, 这里有两个我刚才使用的列子: --查询同一机构的签约数和解约数: select t.sgn_acct_issr_i ...
- Object-C 多线程中锁的使用-NSLock
在多线程的编程环境中,锁的使用必不可少! 于是,今天来总结一下为共享资源加锁的操作方法. 一.使用synchronized方式 //线程1 dispatch_async(dispatch_ge ...
- PhantomJS 一个隐形的浏览器
下载地址: http://phantomjs.org/download.html 使用方法: 下载压缩包解压出来找到phantomjs.exe 放到python的根目录下
- 安装appium需要注意的事项
参考 虫师 的博客园 :http://www.cnblogs.com/fnng/p/4560298.html 1.其中第二篇中,打开命令行用的不是windows中的cmd打开的界面,而是用node. ...