又是两个黑科技一般的存在。

首先我们来介绍一下这两个函数:

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>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
const T& val);
custom (2)
template <class ForwardIterator, class T, class Compare>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp);

作用:

返回一个非递减序列[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>
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,
const T& val);
custom (2)
template <class ForwardIterator, class T, class Compare>
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp);

作用:

返回一个非递减序列[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/

附一道简单例题:

Interesting drink

STL--lower_bound()&upper_bound();的更多相关文章

  1. [STL]lower_bound&upper_bound

    源码 lower_bound template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardI ...

  2. STL lower_bound upper_bound binary-search

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

  3. stl lower_bound upper_bound binary_search equal_range

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

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

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

  5. STL lower_bound upper_bound 用法

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

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

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

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

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

  8. lower_bound && upper_bound

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

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

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

  10. lower_bound/upper_bound example

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

随机推荐

  1. Java生成读取条形码和二维码图片

    原文:http://www.open-open.com/code/view/1453520722495 package zxing; import com.google.zxing.BarcodeFo ...

  2. Activiti 5.17 实体对象与类和数据库表的映射

    一.Activiti 5.17 mybatis的mapping文件声明映射的实体对象关系. <configuration><settings><settingname=& ...

  3. 用户空间&内核空间学习 & top命令 & time命令

    参考了这篇文章 http://www.ruanyifeng.com/blog/2016/12/user_space_vs_kernel_space.html 简单说,Kernel space 是 Li ...

  4. 计算机视觉人脸相关开源项目总结:face_recognition

    计算机视觉人脸相关开源项目总结 https://github.com/ageitgey/face_recognition 深度学习人脸关键点检测方法----综述

  5. sphinx的配置和管理

    网上配置文档众多,但是对着他们的文档来做老是出问题,于是花了点时间研究了一下,写成总结,方便以后查阅.也希望学习sphinx的朋友能少走弯路.Coreseek的安装请参考:http://blog.ch ...

  6. 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂

    浅谈JS中的!=.== .!==.===的用法和区别   var num = 1;     var str = '1';     var test = 1;     test == num  //tr ...

  7. ActiveMQ(五) 转

    package pfs.y2017.m11.mq.activemq.demo05; import javax.jms.Connection; import javax.jms.ConnectionFa ...

  8. bash仅仅读的环境变量

    环境变量名 变量的用途 $0 程序的名字 $1~$9 命令參数1~9的值 $* 全部命令行參数的值 $@ 全部命令行參数的值.假设$@被""包含.即"$@",这 ...

  9. java中类加载顺序(深入Java)

    未涉及虚拟机 0.<init>与<clinit>的区别 1.类的加载过程 2.类的使用方式 3.类的加载来源 4.重载之泛型参数不同可以吗 5.参考 引子 记得上次中秋一哥们写 ...

  10. iOS 开发之 - 关闭键盘 退出键盘 的5种方式

    iOS 开发之 - 关闭键盘 退出键盘 的5种方式   1.点击编辑区以外的地方(UIView) 2.点击编辑区域以外的地方(UIControl) 3.使用制作收起键盘的按钮 4.使用判断输入字元 5 ...