lower_bound()和upper_bound()用法


1、在数组上的用法
假设a是一个递增数组,n是数组长度,则
  • lower_bound(a, a+n, x):返回数组a[0]~a[n-1]中,【大于等于】x的数中,最小的数的指针
  • upper_bound(a, a+n, x):返回数组a[0]~a[n-1]中,【大于】x的数中,最小的数的指针
由于指针可以通过加减算偏移量,所以我们再减去a(数组名会被隐式转换成指针),就得到了相应的下标。
 
对于lower_bound和upper_bound还有一个等价的解释。就是假设我要在a数组中插入x,然后还保持递增,那么
  • lower_bound返回的是x最小的可以插入的数组位置
  • upper_bound返回的是x最大的可以插入的数组位置
 
示例代码:
 #include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a[] = {, , , , , , , , , };
int n = ;
for(int i = ; i < ; i ++)
{
int *lower = lower_bound(a, a+n, i);
int *upper = upper_bound(a, a+n, i);
cout << lower - a << ' ' << upper - a << endl;
}
return ;
}
/*
0 0
1 0
1 3
3 6
6 10
10 10
*/

注:lower_bound( )和upper_bound( )的前两个参数是待查范围的首尾指针(左闭右开区间,不包括尾指针)

 
2、在vector上的用法
vector就相当于一个可以长度可变的数组。当用于vector上时,需要注意以下几点:
  • 前两个参数必须是vector的迭代器
  • 函数的返回值也是迭代器
 vector的迭代器和数组的指针有点类似,比如也可以通过两个迭代器相减算出偏移量,也就是下标。
 
示例代码:
 #include <algorithm>
#include <iostream>
using namespace std; int main()
{
int a[] = {, , , , , , , , , };
int n = ;
vector<int> b(a, a+); for(int i = ; i < ; i ++)
{
vector<int>::iterator lower = lower_bound(b.begin(), b.end(), i);
vector<int>::iterator upper = upper_bound(b.begin(), b.end(), i);
cout << lower - b.begin() << ' ' << upper - b.begin() << endl;
}
return ;
}
/*
0 0
1 0
1 3
3 6
6 10
10 10
*/
 

C++ STL lower_bound()和upper_bound()的更多相关文章

  1. [STL] lower_bound和upper_bound

    STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...

  2. STL源码学习----lower_bound和upper_bound算法

    转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...

  3. STL lower_bound upper_bound binary-search

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

  4. STL中的lower_bound和upper_bound的理解

    STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...

  5. STL 源码分析《5》---- lower_bound and upper_bound 详解

    在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...

  6. STL源码学习----lower_bound和upper_bound算法[转]

    STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...

  7. [转] STL源码学习----lower_bound和upper_bound算法

    http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...

  8. STL之std::set、std::map的lower_bound和upper_bound函数使用说明

    由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...

  9. STL中的二分查找——lower_bound 、upper_bound 、binary_search

    STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...

随机推荐

  1. 第三周 day3 python学习笔记

    1.字符串str类型,不支持修改. 2.关于集合的学习: (1)将列表转成集合set:集合(set)是无序的,集合中不会出现重复元素--互不相同 (2)集合的操作:交集,并集.差集.对称差集.父集.子 ...

  2. hosts配置

    转自:http://www.cnblogs.com/ylemzhang/archive/2011/10/19/2217187.htm 注意: hosts文件不支持端口映射 如果指定端口参考:  //直 ...

  3. ZT 设计模式六大原则(4):接口隔离原则

    设计模式六大原则(4):接口隔离原则 分类: 设计模式 2012-02-27 08:32 17948人阅读 评论(21) 收藏 举报 设计模式classinterfacecstring框架 定义:客户 ...

  4. Mutual Training for Wannafly Union #2

    codeforces 298A. Snow Footprints 分类讨论三种情况: ①..RRRRRR…  ②..LLLLLLL… ③..RRRLLLL.. //AC by lwq: #includ ...

  5. 汇编试验十四:访问CMOS RAM

    CMOS RAM 芯片的特征: 包含一个时钟和一个有128个存储单元的RAM存储器. 该芯片靠电池供电.所以,关机后其内部的时钟仍可正常工作,RAM中的信息不丢失. 128个字节的RAM中,内部时钟占 ...

  6. JavaScript小游戏--翻牌记忆游戏

    翻牌记忆游戏源码 1.有8张图片,每张图片要放两次,生成如下数组,长为16,[0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7] 其中两两相同的代表两张相同的图片,0对应文件夹image ...

  7. VC++读写*.ini配置文件

    ini文件(即Initialization file),这种类型的文件中通常存放的是一个程序的初始化信息.ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Ke ...

  8. HTMLFormElement获取表单里面所有的值然后以json形式返回

    function HTMLFormElement(){ this.init(); return this.json; } HTMLFormElement.prototype.init = functi ...

  9. ng2-bootstrap的modal嵌套时无法滚动的情况

    在ng2-bootstrap的弹窗modal中再弹出另外一个弹窗,关闭子弹窗后,父弹窗会出现无法上下滚动的情况. 通过观察样式可知,关闭子弹窗前,父弹窗的body上是有modal-open样式的,关闭 ...

  10. Vue01 vue基础、mvvm、ES6z知识点、计算属性、生命周期

    Vue案例: <body> <div id="app"> <!--第一部分--> <fieldset> <legend> ...