STL--lower_bound()&upper_bound();
又是两个黑科技一般的存在。
首先我们来介绍一下这两个函数:
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)
返回一个非递减序列[first, last)中的第一个大于等于值val的位置。
ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)
返回一个非递减序列[first, last)中第一个大于val的位置。
如图:

两种函数均采用了二分的方法。
注意:
函数调用序列必须有序。
upper_bound()和lower_bound()的返回值都是迭代器的位置,不能直接与int等类型进行赋值。
std::lower_bound:
| default (1) |
template <class ForwardIterator, class T> |
|---|
| custom (2) |
template <class ForwardIterator, class T, class Compare> |
|---|
作用:
返回一个非递减序列[first, last)中的第一个大于等于值val的位置。
源代码为:
template <class ForwardIterator, class T>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first,last);
while (count>)
{
it = first; step=count/; advance (it,step);
if (*it<val) { // or: if (comp(*it,val)), for version (2)
first=++it;
count-=step+;
}
else count=step;
}
return first;
}
Example:
// lower_bound/upper_bound example
#include <iostream> // std::cout
#include <algorithm> // std::lower_bound, std::upper_bound, std::sort
#include <vector> // std::vector int main () {
int myints[] = {,,,,,,,};
std::vector<int> v(myints,myints+); // 10 20 30 30 20 10 10 20 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector<int>::iterator low,up;
low=std::lower_bound (v.begin(), v.end(), ); // ^
up= std::upper_bound (v.begin(), v.end(), ); // ^ std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; return ;
}
Output:
lower_bound at position
upper_bound at position
资料参考:
http://www.cplusplus.com/reference/algorithm/lower_bound/
std::upper_bound:
| default (1) |
template <class ForwardIterator, class T> |
|---|---|
| custom (2) |
template <class ForwardIterator, class T, class Compare> |
作用:
返回一个非递减序列[first, last)中第一个大于val的位置。
源代码为:
template <class ForwardIterator, class T>
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = std::distance(first,last);
while (count>)
{
it = first; step=count/; std::advance (it,step);
if (!(val<*it)) // or: if (!comp(val,*it)), for version (2)
{ first=++it; count-=step+; }
else count=step;
}
return first;
}
Example:
// lower_bound/upper_bound example
#include <iostream> // std::cout
#include <algorithm> // std::lower_bound, std::upper_bound, std::sort
#include <vector> // std::vector int main () {
int myints[] = {,,,,,,,};
std::vector<int> v(myints,myints+); // 10 20 30 30 20 10 10 20 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector<int>::iterator low,up;
low=std::lower_bound (v.begin(), v.end(), ); // ^
up= std::upper_bound (v.begin(), v.end(), ); // ^ std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; return ;
}
Output:
lower_bound at position
upper_bound at position
资料参考:
http://www.cplusplus.com/reference/algorithm/upper_bound/
附一道简单例题:
STL--lower_bound()&upper_bound();的更多相关文章
- [STL]lower_bound&upper_bound
源码 lower_bound template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardI ...
- STL lower_bound upper_bound binary-search
STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...
- stl lower_bound upper_bound binary_search equal_range
自己按照stl实现了一个: http://www.cplusplus.com/reference/algorithm/binary_search/ 这里有个注释,如何判断两个元素相同: Two e ...
- 鬼知道是啥系列之——STL(lower_bound(),upper_bound() )
引子,不明觉厉: 百度,渐入佳境: 头铁,入门到放弃: lower_bound(): 头文件: #include<algorithm>函数功能: 函数lower_bound()在f ...
- STL lower_bound upper_bound 用法
1.lower_bound(begin,end,x) 返回第一个>=x的位置,找不到return .end() 2.upper_bound (begin,end,x) 返回第一个>x的位置 ...
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- STL中的unique()和lower_bound ,upper_bound
unique(): 作用:unique()的作用是去掉容器中相邻元素的重复元素(数组可以是无序的,比如数组可以不是按从小到大或者从大到小的排列方式) 使用方法:unique(初始地址,末地址): 这里 ...
- lower_bound && upper_bound
用lower_bound进行二分查找 ●在从小到大排好序的基本类型数组上进行二分查找. 这是二分查找的一种版本,试图在已排序的[first,last)中寻找元素value.如果[first,last ...
- C++ lower_bound/upper_bound用法解析
1. 作用 lower_bound和upper_bound都是C++的STL库中的函数,作用差不多,lower_bound所返回的是第一个大于或等于目标元素的元素地址,而upper ...
- lower_bound/upper_bound example
http://www.cplusplus.com/reference/algorithm/upper_bound/左闭右开 Return iterator to lower bound Returns ...
随机推荐
- 50 个 Bootstrap 插件
Bootstrap是快速开发Web应用程序的前端工具包.它是一个CSS和HTML的集合,它使用了最新的浏览器技术,给你的Web开发提供了时尚的版式,表单,buttons,表格,网格系统等等. 本文向你 ...
- 基于Office 365 无代码工作流分析-表单基本需求分析!
3.2表单的制作 基于下图的需求,我们须要定义例如以下的表单列表:
- 从Nginx源代码谈大写和小写字符转化的最高效代码以及ASCII码表的科学
说起大写和小写字母转换.大家非常easy想起系统函数是不是,差点儿全部的编程语言都提供了这样的转换函数,可是你有没有想过这背后是怎么实现的? 让你写怎么实现? 我们都知道Nginx是眼下用的最多的Ht ...
- Eclipse中Easy Shell插件配置PowerCmd
1.了解EasyShell.PowerCMD和基本命令 http://sourceforge.net/projects/pluginbox/ http://www.powercmd.com/ 了解基本 ...
- CPU Stepping
http://baike.baidu.com/view/16839.htm?fr=ala0_1_1 步进 编辑 步进(Stepping)是CPU的一个重要参数,也叫分级鉴别产品数据转换规范,“步进 ...
- 不能选择sublime作为默认打开方式的解决办法
Sublime Text 绿色版删除后无法设置为默认打开方式…而且网上也没有给出明确的解决办法 注册表的解决办法: 删除 HKEY_CURRENT_USER\Software\Classes\Appl ...
- 数组index
1. 数组index与数组名的位置关系 a[b] = *(a + b) = *(b + a) = b[a] int a[5] = {1, 2, 3, 4, 5}; printf("% ...
- SignatureDoesNotMatch REST接口 在任何时间、任何地点、任何互联网设备上 在Header中包含签名
PutObject_关于Object操作_API 参考_对象存储 OSS-阿里云 https://help.aliyun.com/document_detail/31978.html OSS API ...
- hdfs 3种 通讯协议
http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_design.html 通讯协议 所有的HDFS通讯协议都是建立在TCP/IP协议之上.客户端通过一个可配置的 ...
- https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2