[STL]lower_bound&upper_bound
源码
lower_bound
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>0)
{
it = first; step=count/2; advance (it,step);
if (*it<val) { // or: if (comp(*it,val)), for version (2)
first=++it;
count-=step+1;
}
else count=step;
}
return first;
}
upper_bound
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>0)
{
it = first; step=count/2; std::advance (it,step);
if (!(val<*it)) // or: if (!comp(val,*it)), for version (2)
{ first=++it; count-=step+1; }
else count=step;
}
return first;
}
\ 看起来就是个二分,不过有点奇怪
用途
\ lower_bound
\ "returns an iterator pointing to the first element in the range [first,last) which does not compare less than val."
\ 返回一个序列中第一个不小于val值的参数。注意该序列必须已经排序
\ upper_bound
\ "returns an iterator pointing to the first element in the range [first,last) which compares greater than val."
\ 返回一个序列中第一个严格大于val值的参数。注意该序列必须已经排序
\ "Unlike lower_bound, the value pointed by the iterator returned by this function cannot be equivalent to val, only greater."
\ 不想lower_bound,upper_bound只返回严格大于val的值
参数
first, last
\ Forward iterators to the initial and final positions of a sorted (or properly partitioned) sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
\ 在一个已经排序的序列中传入起始和结束的迭代器。程序的范围为\([first,last)\),也就是说包含第一个迭代器指向的值,但不包含最后一个迭代器指向的值。
val
\ Value of the lower bound to search for in the range.
\ lowerbound要在范围内找的那个数
\ For (1), T shall be a type supporting being compared with elements of the range [first,last) as the right-hand side operand of operator<.
\ val的类型必须能和序列中的元素用\(<\)比较
comp
\ Binary function that accepts two arguments (the first of the type pointed by ForwardIterator, and the second, always val), and returns a value convertible to bool. The value returned indicates whether the first argument is considered to go before the second.
\ 返回值为bool的程序并且有两个参数(第一个值,第二个值)。返回第一个参数是否应该优先于第二个参数处理。其实就是判断小于
\ The function shall not modify any of its arguments.
\ 这个函数不能修改仍和他的传入参数。显而易见
\ This can either be a function pointer or a function object.
\ 珂以是一个函数的指针,也珂以是一个函数的实体
时间复杂度
Performs approximately \(log_2(N)+1\)
[STL]lower_bound&upper_bound的更多相关文章
- 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 ...
随机推荐
- vue组件父子间通信之综合练习--假的聊天室
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- ElasticSearch 基础 1
ElasticSearch 基础=============================== 索引创建 ========================== 1. RESTFUL APIAPI 基本 ...
- from 表单回车自动提交
自动提交的情况 1 表单只有单个输入框 2 type=‘submit 这里注意button默认type为submit 解决方法 1 添加一个隐藏的输入框 2 form添加属性 onsubmit=&q ...
- Java设计模式——模板方法设计模式(abstract修饰)
解释: 一个抽象类中,有一个主方法,再定义 1...n 个方法,可以是抽象的,也可以是实际的方法,定义一个类,继承该抽象类,重写抽象方法,通过调用抽象类,实现对子类的调用. 解决的问题: 当功能内部一 ...
- Lucene 4.6.1 java.lang.IllegalStateException: TokenStream contract violation
这是旧代码在新版本Lucene中出现的异常,异常如下: Exception in thread "main" java.lang.IllegalStateException: To ...
- mysql远程命令连接
#mysql -h 服务器地址 -P 端口 -u账号 -p密码 mysql -uroot -proot
- Lambda创建表达式目录树
一,如下代码: using System; using System.Linq.Expressions; namespace Demo { class Program { static void Ma ...
- Latex--入门系列二
Latex 专业的参考 tex对于论文写作或者其他的一些需要拍版的写作来说,还是非常有意义的.我在网上看到这个对于Latex的入门介绍还是比较全面的,Arbitrary reference.所以将会翻 ...
- 使用控制台搭建vue-cli脚手架
注意: 1.安装前您需要查看自己是否有node环境 检查:node - v 2.如果没有的话,需要先搭建好才能进行下一步操作 (参考:https://www.cnblogs.com/sylys/p/ ...
- 没有找到<context:component-scan base-package="">标签
<?xml version="1.0" encoding="UTF-8"?> <!-- 指定Spring配置文件的Schema信息 --> ...