关于lower_bound( )和upper_bound( )的常见用法
头文件:#include<algorithm>
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的。
在从小到大的排序数组中,
lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
在从大到小的排序数组中,重载lower_bound()和upper_bound()
lower_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
#include<bits/stdc++.h>
using namespace std;
const int maxn=+;
const int INF=*int(1e9)+;
#define LL long long
int cmd(int a,int b){
return a>b;
}
int main(){
int num[]={,,,,,};
sort(num,num+); //按从小到大排序
int pos1=lower_bound(num,num+,)-num; //返回数组中第一个大于或等于被查数的值
int pos2=upper_bound(num,num+,)-num; //返回数组中第一个大于被查数的值
cout<<pos1<<" "<<num[pos1]<<endl;
cout<<pos2<<" "<<num[pos2]<<endl;
sort(num,num+,cmd); //按从大到小排序
int pos3=lower_bound(num,num+,,greater<int>())-num; //返回数组中第一个小于或等于被查数的值
int pos4=upper_bound(num,num+,,greater<int>())-num; //返回数组中第一个小于被查数的值
cout<<pos3<<" "<<num[pos3]<<endl;
cout<<pos4<<" "<<num[pos4]<<endl;
return ;
}
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int a[] = {, , , , , };
//int pos = *lower_bound(a, a + 6, 7);
//int res = *upper_bound(a, a + 6, 7);
//cout << pos << endl;
//cout << res << endl;
cout << &a[] - a<< endl;
cout << lower_bound(a, a + , ) - a<< endl;
return ; }

原作者:https://blog.csdn.net/qq_40160605/article/details/80150252
求长度为 n 的有序数组 a 中 k 的个数 :
upper_bound(a, a+n, k) - lower_bound(a, a+n, k)
关于lower_bound( )和upper_bound( )的常见用法的更多相关文章
- lower_bound( )和upper_bound( )的常见用法
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...
- lower_bound( )和upper_bound( )的基本用法
lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end.通过返回的地址减去起始地址 ...
- lower_bound 和 upper_bound 功能和用法
以前用这两个函数的时候,简单看了几句别人的博客,记住了大概,用的时候每用一次就弄混一次,相当难受,今天对照着这两个函数的源码和自己的尝试发现:其实这两个函数只能用于 "升序" 序列 ...
- 关于lower_bound()和upper_bound()
关于lower_bound()和upper_bound(): 参考:关于lower_bound( )和upper_bound( )的常见用法 注意:查找的数组必须要是排好序的.因为,它们查找的方式也是 ...
- lower_bound和upper_bound的实现和基本用法
最近一直在学dp,但是感觉进度明显慢了很多,希望自己可以加一把劲,不要总是拖延了... 在LIS的优化中我遇到了二分查找的问题,之前也知道lower_bound和upper_bound两个函数,但是没 ...
- lower_bound()和upper_bound()用法详解
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. lower_bound( begin,end,num):从数组的begin位置到end ...
- 【模板】关于vector的lower_bound和upper_bound以及vector基本用法 STL
关于lower_bound和upper_bound 共同点 函数组成: 一个数组元素的地址(或者数组名来表示这个数组的首地址,用来表示这个数组的开头比较的元素的地址,不一定要是首地址,只是用于比较的& ...
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...
- C++ STL lower_bound()和upper_bound()
lower_bound()和upper_bound()用法 1.在数组上的用法 假设a是一个递增数组,n是数组长度,则 lower_bound(a, a+n, x):返回数组a[0]~a[n-1]中, ...
随机推荐
- ES6,ES5,ES3,对比学习~
在简书上看到一个博主写的文章,感觉很有用.留下:https://www.jianshu.com/p/287e0bb867ae Excuse me?这个前端面试在搞事!https://zhuanlan ...
- py001
pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple -------------------------------- ...
- Generator自动生成DAO和POJO代码
一 添加相关插件 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>myba ...
- Git使用和Vue项目
1.创建git排除文件,.gitignore 2.READEME.md 和 LICENSE开源协议 git init 创建仓库 , git status 查看文件状态 红色文件表示未提交. git ...
- 【数据结构】算法 LinkList (Merge Two Sorted Lists)
合并2个有序链表 list A, list B, Solution: 对A,B 表按序读取数据,比较大小后插入新链表C. 由于两个输入链表的长度可能不同,所以最终会有一个链表先完成插入所有元素,则直接 ...
- nmon安装与使用
一.检查安装环境 1,# uname –a (查看操作系统信息,所检查服务器为64位操作系统) Linux jmeter 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 ...
- python python中那些双下划线开头的那些函数都是干啥用用的
1.写在前面 今天遇到了__slots__,,所以我就想了解下python中那些双下划线开头的那些函数都是干啥用用的,翻到了下面这篇博客,看着很全面,我只了解其中的一部分,还不敢乱下定义. 其实如果足 ...
- vue webpack 引入iview iview内部文件报语法错误
错误如下: 是因为 es6 语法没有成功转化 因为 我的项目是别人做好的已经部署的项目 ,但是用到的是es2015 配置es2015并不起作用 是因为es2015已经过期了 安装的时候回有类似下面的 ...
- 利用composer安装laraval
首先,毋庸置疑我们需要安装composer.这个在我上一篇文章中有提到,这里不做过多赘述. 其次,配置composer国内镜像.(如果不配置国内镜像,你们懂得) 打开cmd输入以下命令即可 compo ...
- XUnit测试框架-Python unittest
选择 语言选择 本次个人作业我选择的语言是Python,了解学习Python有一段时间了但是一直没有练习,所以这次玩蛇,使用的版本是Python3.6. 开发工具选择 我选择的IDE是Pycharm, ...