源码

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的更多相关文章

  1. STL lower_bound upper_bound binary-search

    STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...

  2. stl lower_bound upper_bound binary_search equal_range

    自己按照stl实现了一个:   http://www.cplusplus.com/reference/algorithm/binary_search/ 这里有个注释,如何判断两个元素相同: Two e ...

  3. 鬼知道是啥系列之——STL(lower_bound(),upper_bound() )

    引子,不明觉厉:   百度,渐入佳境: 头铁,入门到放弃: lower_bound(): 头文件:  #include<algorithm>函数功能:  函数lower_bound()在f ...

  4. STL lower_bound upper_bound 用法

    1.lower_bound(begin,end,x) 返回第一个>=x的位置,找不到return .end() 2.upper_bound (begin,end,x) 返回第一个>x的位置 ...

  5. STL中的二分查找———lower_bound,upper_bound,binary_search

    关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...

  6. STL中的unique()和lower_bound ,upper_bound

    unique(): 作用:unique()的作用是去掉容器中相邻元素的重复元素(数组可以是无序的,比如数组可以不是按从小到大或者从大到小的排列方式) 使用方法:unique(初始地址,末地址): 这里 ...

  7. lower_bound && upper_bound

     用lower_bound进行二分查找 ●在从小到大排好序的基本类型数组上进行二分查找. 这是二分查找的一种版本,试图在已排序的[first,last)中寻找元素value.如果[first,last ...

  8. C++ lower_bound/upper_bound用法解析

    1. 作用           lower_bound和upper_bound都是C++的STL库中的函数,作用差不多,lower_bound所返回的是第一个大于或等于目标元素的元素地址,而upper ...

  9. lower_bound/upper_bound example

    http://www.cplusplus.com/reference/algorithm/upper_bound/左闭右开 Return iterator to lower bound Returns ...

随机推荐

  1. spring map获取同类型的bean

    今天看博客怎么减少if else 方法, 才发现spring 还有很多功能我没有用到,以后真的得花时间学学spring,今天学到的东西如下: 1.定义一个接口 store public interfa ...

  2. 【FICO系列】SAP 关于SAP中的记账码的解释

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP 关于SAP中的记账码的解 ...

  3. 好的计数思想-LightOj 1213 - Fantasy of a Summation

    https://www.cnblogs.com/zhengguiping--9876/p/6015019.html LightOj 1213 - Fantasy of a Summation(推公式 ...

  4. dataTables使用的详细说明整理

    本文共三个部分:官网|基本使用|遇到的问题 一.官方网站:http://www.datatables.club/ 二.基本使用: 1.dataTables的引入及初始化 <!--第一步:引入Ja ...

  5. Maven-maven安装、Eclipse配置maven

    1.下载maven安装包,下载完成,解压到安装路径. 2.配置环境变量 3.修改setting.xml配置本地库,阿里云中央仓库 路径:C:\fyliu\software\apache-maven-3 ...

  6. java学习day3运算符

    一.算数运算符 1.对于除号“/”,它的整数除和小数除是有区别的:当整数除以整数的时候,会把结果的小数部分舍弃,只保留整数部分,例如: int x=3510; x=x/1000; 输出结果为x=3; ...

  7. 什么是http协议??

    一.http协议的定义: http(Hypertext transfer protocol)超文本传输协议,通过浏览器和服务器进行数据交互,进行超文本(文本.图片.视频等)传输的规定.也就是说,htt ...

  8. 浏览器是怎样工作的:渲染引擎,HTML解析(连载二)

    转载自:http://ued.ctrip.com/blog/how-browsers-work-rendering-engine-html-parsing-series-ii.html 渲染引擎 渲染 ...

  9. 灵活的理解JavaScript中的this指向(一)

    this是JavaScript中的关键字之一,在编写程序的时候经常会用到,正确的理解和使用关键字this尤为重要.首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确 ...

  10. websocket无法注入bean问题解决方案

    websocket服务端往往需要和服务层打交道,因此需要将服务层的一些bean注入到websocket实现类中使用,但是呢,websocket实现类虽然顶部加上了@Component注解,依然无法通过 ...