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. photoshopcs6破解补丁用来干嘛的

    photoshopcs6破解补丁为 Adobe CS6 系列软件通用破解补丁,亲测可用,终于能用了不再出现那个烦人的购买页面了,cs6破解补丁解压后得到32和64两个文件夹,根据自己的系统类型选择,6 ...

  2. 040同步条件event

    条件同步和条件变量同步差不多意思,只是少了锁功能,因为条件同步设计于不访问共享资源的条件环境,event=threading.Event():条件环境对象,初始值为False.event.isSet( ...

  3. 使用ant进行邮件发送,ant发送已存在的html文件

    Jenkins上使用发送邮件功能一直有问题,放弃Jenkins配置,使用ant的进行发送邮件,参考文档可以: https://www.jianshu.com/p/04cfce59890a 我这里是要发 ...

  4. hdu-2837 Calculation---指数循环节

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2837 题目大意: 已知f(0) = 1,0^0 =1,[注意,0的其他任意次方为0,虽然题没有直接给 ...

  5. 使用Fragment填充ViewPager

    在上一篇文章中,讲解了使用PagerAdapter作为适配器时的ViewPager的使用方法.然后在实际项目中更多的使用Fragment作为页卡,因为实际开发中每一个页卡要复杂的多.而使用Fragme ...

  6. Django 导出csv文件 中文乱码问题

    import csvimport codecsimport datetimefrom django.db import connectionfrom django.contrib.auth.model ...

  7. CRT公钥登录

    1.实现原理: 通过CRT生成的密钥对,把公钥上传到Linux服务器指定用户下的.ssh目录中,在客户端上只需输入秘钥的密码即可登陆,而且验证一次以后可以免密码登陆 2.具体过程: 转自:http:/ ...

  8. Swift3.0 调用C函数-_silen_name

    一般情况下Swit要想调用obj-c,c或c++代码必须通过obj-c以及桥接文件才可以办到,但是使用@_silgen_name,可以对于某些简单的代码,直接跳过桥接文件和.h头文件与C代码交互. 创 ...

  9. C++ 全局变量不明确与 using namespace std 冲突

    写了个汉诺塔,使用全局变量count来记录步数,结果Error:count不明确 #include <iostream> using namespace std; ; void hanoi ...

  10. [19/04/03-星期三] IO技术_其它流(RandomAccessFile 随机访问流,SequenceInputStream 合并流)

    一.RandomAccessFile 随机访问流 [版本1] /* *RandomAccessFile 所谓随机读取就是 指定位置开始或指定位置结束 的读取写入文件 * 实现文件的拆分与合并 模拟下载 ...