头文件:#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( )的常见用法的更多相关文章

  1. lower_bound( )和upper_bound( )的常见用法

    lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...

  2. lower_bound( )和upper_bound( )的基本用法

    lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end.通过返回的地址减去起始地址 ...

  3. lower_bound 和 upper_bound 功能和用法

    以前用这两个函数的时候,简单看了几句别人的博客,记住了大概,用的时候每用一次就弄混一次,相当难受,今天对照着这两个函数的源码和自己的尝试发现:其实这两个函数只能用于 "升序" 序列 ...

  4. 关于lower_bound()和upper_bound()

    关于lower_bound()和upper_bound(): 参考:关于lower_bound( )和upper_bound( )的常见用法 注意:查找的数组必须要是排好序的.因为,它们查找的方式也是 ...

  5. lower_bound和upper_bound的实现和基本用法

    最近一直在学dp,但是感觉进度明显慢了很多,希望自己可以加一把劲,不要总是拖延了... 在LIS的优化中我遇到了二分查找的问题,之前也知道lower_bound和upper_bound两个函数,但是没 ...

  6. lower_bound()和upper_bound()用法详解

    lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. lower_bound( begin,end,num):从数组的begin位置到end ...

  7. 【模板】关于vector的lower_bound和upper_bound以及vector基本用法 STL

    关于lower_bound和upper_bound 共同点 函数组成: 一个数组元素的地址(或者数组名来表示这个数组的首地址,用来表示这个数组的开头比较的元素的地址,不一定要是首地址,只是用于比较的& ...

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

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

  9. C++ STL lower_bound()和upper_bound()

    lower_bound()和upper_bound()用法 1.在数组上的用法 假设a是一个递增数组,n是数组长度,则 lower_bound(a, a+n, x):返回数组a[0]~a[n-1]中, ...

随机推荐

  1. 最新阿里云服务器免费SSL证书配置HTTPS的两种方法(图文教程二)

    在大家学习如何利用免费SSL证书配置网站HTTPS之前,我们先要搞清楚为什么要开启HTTPS,这个绿色的小锁真的有用吗?所谓的HTTPS其实是(安全套接字层超文本传输协议)是以安全为目标的HTTP通道 ...

  2. python 练习1(流控制)

    #!/usr/bin/python #_*_ coding:utf-8 _*_ #练习题 #1.使用while循环输入 1 2 3 4 5 6 8 9 10 #a.定义一个变量存放数字 #b.用whi ...

  3. git远程管理

  4. linux 邮件服务器—Extmail

    环境: Centos 6.5 :172.16.9.13 (DNS 服务器) Centos 6.5: 172.16.9.11 (postfix 邮件服务器) 安装软件: yum -y install p ...

  5. 【论文速读】Sheng Zhang_AAAI2018_Feature Enhancement Network_A Refined Scene Text Detector

    Sheng Zhang_AAAI2018_Feature Enhancement Network_A Refined Scene Text Detector 作者 关键词 文字检测.水平文字.Fast ...

  6. Mysql AVG() 值 返回NULL而非空结果集

    [1]select 查询返回一行NULL 先来模拟复现一下遇到的问题. (1)源数据表grades,学生成绩表 (2)查询SQL语句 查询‘080601’班的各门课平均成绩 SELECT sClass ...

  7. C#匿名类型和动态解析减少定义传输类模板

    C#作为强类型语言,在序列化和反序列化(json)场景中对字符串解析常常需要定义强类型模板,造成编码上的繁琐.其实可以使用匿名类型和动态解析减少json序列化时候的数据模板定义: string a = ...

  8. netperf

    官网 下载 装包 yum -y install make automake libtool pkgconfig libaio-devel 编译安装 ./autogen.sh ./configure m ...

  9. 自动弹出pickerview

    UIPickerView是开发中常用的控件,日期选择.年龄选择.城市的多级联动等等都会使用,它一般是在点击某个按钮后出现,展现方式和UITextView一样,从页面底部弹出,选中后或者点击控件以外区域 ...

  10. 进行分支切换时,出现error的修复方法

    进行分支切换时,出现如此错误,导致无法正常切换:error: The following untracked working tree files would be overwritten by ch ...