源码

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\)

最新文章

  1. APP漏洞扫描用地址空间随机化
  2. linux的学习记录随笔
  3. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
  4. HDU 2087  KMP模板题
  5. TextView中的部分文字响应点击事件
  6. Android 配置问题
  7. 页面显示(pageshow)和页面隐藏(pagehide)事件
  8. Vertica 项目常用代码
  9. 一个socket发送调试信息的类
  10. windows 开机启动(为了关闭虚拟机的那么多开机进程)
  11. Django web 开发指南 no such table:
  12. 学习restful 架构
  13. 在Ubuntu上安装使用Systemtap
  14. Centos ssh 登陆乱码解决办法
  15. Java date
  16. java + tomcat cookie 异常
  17. 小A与小B-(双向bfs)
  18. uva11916 Emoogle Grid (BSGS)
  19. linux 网络命令
  20. 利用Ajax和JSON实现关于查找省市名称的二级联动功能

热门文章

  1. Oracle数据备份与恢复
  2. Delphi XE2 之 FireMonkey 入门(14) - 滤镜: 概览
  3. 用python进行月份加减的函数
  4. Tensorflow | 基本函数介绍 简单详细的教程。 有用, 很棒
  5. .net任务调度平台 Dyd.BaseService.TaskManager
  6. Java ——Scanner
  7. nginx--&gt;基本使用
  8. 如何让cmd启动始终以管理员身份运行(方法已失效)
  9. zend studio远程自动上传代码并执行
  10. 键盘按键KeyCode大全