C++ STL lower_bound()和upper_bound()
lower_bound()和upper_bound()用法
- lower_bound(a, a+n, x):返回数组a[0]~a[n-1]中,【大于等于】x的数中,最小的数的指针
- upper_bound(a, a+n, x):返回数组a[0]~a[n-1]中,【大于】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( )的前两个参数是待查范围的首尾指针(左闭右开区间,不包括尾指针)
- 前两个参数必须是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()的更多相关文章
- [STL] lower_bound和upper_bound
STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- STL lower_bound upper_bound binary-search
STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...
- STL中的lower_bound和upper_bound的理解
STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...
- STL 源码分析《5》---- lower_bound and upper_bound 详解
在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...
- STL源码学习----lower_bound和upper_bound算法[转]
STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...
- [转] STL源码学习----lower_bound和upper_bound算法
http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...
随机推荐
- Master的注册机制和状态改变管理解密
本课主题 Master 接收 Worker, Driver, Application 注册 Master 处理 Driver 狀态变换 Master 处理 Executor 狀态变换 Master 接 ...
- windows生成硬链接
因工作电脑需要同时使用pl/sql和toad工具需要同时配置32位和64位oracle client如此增加了维护tnsnames.ora的复杂程度使用windows硬链接可以减少工作量,每次只修改源 ...
- ABAP类的文档生成工具
本文介绍的工具原理和JavaDoc类似,位于software component BBPCRM 1. 在ABAP类里新建一个名为CLASS_DOCU的私有方法. 然后把所有的注释写在这个方法源代码的前 ...
- 入职日志——Solomon
心情 今天是所有培训结束后入职的第二天,紧张且期待. 紧张是因为昨天董经理有句话点醒了我. 你默默不再是个学生了.没有人会像你的老师父母一样担待你,原谅你. 职场是残酷无情的,是以结果为导向的.不论对 ...
- Visual Studio中头文件stdafx.h的作用
在较新版的Visual Studio中,新生成的C++项目文件的的头文件夹下会默认有头文件stdafx.h,而源文件夹下则默认有源文件stdafx.cpp,手动将这些文件删除后,编译时系统还会报错.下 ...
- Java程序员从笨鸟到菜鸟之(九十六)深入java虚拟机(五)——java本地接口JNI详解
http://blog.csdn.net/csh624366188/article/details/8063144 对于Java程序员来说,java语言的好处和优点,我想不用我说了,大家自然会说出很多 ...
- BZOJ3790:神奇项链(Manacher)
Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字 母组成的字符串,每个小写字母表示一种颜色.为了制作这个项链,小 H 购买了两个机器.第一个机器可 ...
- maven 根据P参数值打包动态修改properties文件中值或一定properties
需求:由于最近开发clover项目 ,没有使用spring,更没有使用任何框架,而使用J2EE的web工程,所以连接ZK和MongoDB.Redis等服务器需用指定properties文件, 而目前公 ...
- JedisConnectionException: Failed connecting to host localhost:6379
报错原因:没有启动服务,打开服务即可 redis.clients.jedis.exceptions.JedisConnectionException: Exception at redis.clien ...
- android中OpenMax的实现【2】AwesomePlayer中openmax的入口
AwesomePlayer 中有个变量 OMXClient mClient; 让我们看看 OMXClient class OMXClient { public: OMXClient(); stat ...