lower_bound( )和upper_bound( )的常见用法
ower_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() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num,greater() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
const int INF=2*int(1e9)+10;
#define LL long long
int cmd(int a,int b){return a>b;
}
int main(){int num[6]={1,2,4,7,15,34}; sort(num,num+6); //按从小到大排序 int pos1=lower_bound(num,num+6,7)-num; //返回数组中第一个大于或等于被查数的值 int pos2=upper_bound(num,num+6,7)-num; //返回数组中第一个大于被查数的值cout<<pos1<<" "<<num[pos1]<<endl;cout<<pos2<<" "<<num[pos2]<<endl;sort(num,num+6,cmd); //按从大到小排序int pos3=lower_bound(num,num+6,7,greater<int>())-num; //返回数组中第一个小于或等于被查数的值 int pos4=upper_bound(num,num+6,7,greater<int>())-num; //返回数组中第一个小于被查数的值 cout<<pos3<<" "<<num[pos3]<<endl;cout<<pos4<<" "<<num[pos4]<<endl;return 0;
}
lower_bound( )和upper_bound( )的常见用法相关推荐
- upperbound找不到_关于lower_bound( )和upper_bound( )的常见用法
头文件:#include lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begi ...
- 关于lower_bound( )和upper_bound( )的常见用法
ower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数组 ...
- lower_bound( )和upper_bound( )的基本用法
lower_bound( )和upper_bound( )是C++ STL模板库中的函数,其作用可以看作是一个简单的二分模板. 基本使用方法: lower_bound: 参数:lower_bound( ...
- 【ACM】Uva 1152 (4 Values whose Sum is 0) 二分查找lower_bound() 和upper_bound()的使用
[问题描述] The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, ...
- lower_bound 和 upper_bound的区别
转载至某个比较好看的博客:关于lower_bound( )和upper_bound( )的常见用法 下面的内容: lower_bound( )和upper_bound( )都是利用二分查找的方法在一个 ...
- lower_bound和 upper_bound 用法(STL)
lower_bound和 upper_bound的头文件是#include<algorithm> lower_bound 返回第一个大于等于 x 的数的地址/迭代器 upper_bound ...
- lower_bound和upper_bound的用法
二分查找算法可以解决最简单的二分查找问题:a数组单调递增,并且其中没有重复的数值.我们遇到的实际问题可能就没有这么简单,可能会有重复的数值.比如a数组里有3个5.这时我们查找5就有一个问题:到底返回哪 ...
- lower_bound( )和upper_bound( )的返回值及用法
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的 在从小到大的排序数组中, lower_bound( begin,end,num):从数组 ...
- STL剖析(二):容器底层数据结构及常见用法
一.概述 本文主要聚焦于STL容器,STL完整的容器分类体系如下所示,下文将逐一对各个容器底层的数据结构以及常见用法进行介绍. 测试环境:Ubuntu 22.04 g++ 11.3.0 二.顺序容器 ...
最新文章
- TensorFlow 2.2.0-rc0,这次更新让人惊奇!
- 输入一个链表,反转链表后,输出新链表的表头(ACM格式)(美团面试题)
- 计算机动画制作 实验要求,A074计算机动画制作实验——动画制作初步
- 067 Add Binary 二进制求和
- 笑傲江湖,独孤求败-NetScaler MAS应用交付神功详解
- 填空题C语言,c语言填空题试卷.doc
- pstack 安装linux_详解命令-pstack
- 内参、外参、畸变参数三种参数与相机的标定方法与相机坐标系的理解
- 【社招持续篇】云和恩墨虚位以待,你来不来!
- 神经网络入门之RNN(三)
- javascript实现页面自动刷新和自动跳转代码
- makeinfo:未找到命令
- c# PropertyGrid 自定义属性排序
- 【bat】批处理中的行,段注释
- 系统重构过程中的异构数据同步回环处理
- shimano 型号详解 (zz)
- 服务器网卡驱动_教你星际蜗牛C款i211网卡服务器咋装Windows sevs2012R2服务器下
- 2014 年总结--【宽容待人】
- DxO PhotoLab 2.0完整汉化版|DxO PhotoLab 2.0中文版(WinX64)
- bugku-细心(想办法变成admin)
热门文章
- 还在为产品的客户服务而烦恼?来搭建在线客服中心!
- 如何处理u盘一插进电脑就自动打开里面的所有文件夹
- 基于 Windows系统的 KingbaseES 数据库软件安装指南(3. 安装前准备工作)
- Redis中使用Lua脚本(续)- Linux下Lua-cjson开源库的安装和使用
- 《AngularJS深度剖析与最佳实践》简介
- 人工智能导论——遗传算法求解TSP问题实验
- C# 名称空间的别名
- 学习中的一些笔记,不懂的时候再来翻翻(持续更新)
- 遇见狂神说HTML笔记1
- html+canvas+输入文字换行,canvas文字换行