源码

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. mysql analyze和optimize

    Analyze Table MySQL 的Optimizer(优化元件)在优化SQL语句时,首先需要收集一些相关信息,其中就包括表的cardinality(可以翻译为“散列程度”),它表示某个索引对应 ...

  2. Session设置

    from django.shortcuts import render, redirect from django import views # Create your views here. fro ...

  3. 解决:”ssh-keygen 不是内部或外部命令“ 的问题

    相信大家在 码云生成/添加SSH公钥的过程中遇到一个比较常见的问题, 在cmd,命令行输入 ssh-keygen -t rsa -C "xxxxx@xxxxx.com" ; xxx ...

  4. [Codeforces 1199C]MP3(离散化+二分答案)

    [Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...

  5. selenium安装及环境搭建

    说明:安装selenium前提必须是安装好了python和pip 1.安装python 在Python的官网 www.python.org 中找到最新版本的Python安装包(我的电脑是windows ...

  6. day20 博客系统开发

    setting 文件加入  AUTH_USER_MODEL = "app名称.UserInfo"     from django.db import models # Create ...

  7. git把本地代码上传(更新)到github上

    # 初始化目录为本地仓库 git init # 添加所有文件到暂存去 git add . # 提交所有文件 git commit -m "init" # 添加远程仓库地址 git ...

  8. vue-cli-webpake搭建和配置

    确认创建项目后,后续还需输入一下项目名称.项目描述.作者.打包方式.是否使用ESLint规范代码等等,详见上图.安装顺利执行后会,生成如下文件目录:1.全局化安装cnpm npm install cn ...

  9. windows下使用命令行获取管理员权限

    在win下运行npm install安装依赖出现错误: Error: EBUSY, resource busy or locked 搜索错误信息后发现是由于没有管理员权限,在bash中输入以下命令后运 ...

  10. 2018-9-21-dot-net-core-使用-usb

    title author date CreateTime categories dot net core 使用 usb lindexi 2018-09-21 19:53:34 +0800 2018-0 ...