二分搜索:lower_bound 与 upper_bound 函数
基本用法:
lower_bound 和upper_bound需要用在一个有序数组和容器中。
(1)lower_bound (first,last,val)用来寻找在数组和容器[first,last)的范围内,第一个值大于等于val的元素的位置。如果是数组,则返回该位置的指针,如果是容器,则返回该位置的迭代器。
(2)upper_bound(first,last,val)用来寻找在数组和容器[first,last)的范围内,第一个值大于val的元素的位置。如果是数组,则返回该位置的指针,如果是容器,则返回该位置的迭代器。
显然,如果数组和容器中没有需要寻找的元素。lower_bound 和upper_bound均返回可以插入该元素的位置的指针和迭代器(即假设存在该元素时,该元素应当在的位置)。
头文件:algorithm
复杂度:lower_bound 和upper_bound均为O()
示例如下:
#include<iostream>
#include<algorithm>
using namespace std;
int main(){int a[10]={1,2,2,3,3,3,5,5,5,5};int *lowerpos=lower_bound(a,a+10,-1);int *upperpos=upper_bound(a,a+10,-1);cout<<lowerpos-a<<","<<upperpos-a<<endl;//寻找1lowerpos=lower_bound(a,a+10,1);upperpos=upper_bound(a,a+10,1);cout<<lowerpos-a<<","<<upperpos-a<<endl;//寻找3lowerpos=lower_bound(a,a+10,3);upperpos=upper_bound(a,a+10,3);cout<<lowerpos-a<<","<<upperpos-a<<endl;//寻找4lowerpos=lower_bound(a,a+10,4);upperpos=upper_bound(a,a+10,4);cout<<lowerpos-a<<","<<upperpos-a<<endl;//寻找6lowerpos=lower_bound(a,a+10,6);upperpos=upper_bound(a,a+10,6);cout<<lowerpos-a<<","<<upperpos-a<<endl;return 0;
}
输出结果:
0,0
0,1
3,6
6,6
10,10
如果只是想获得欲查元素的下标
显然,就可以不使用临时指针,而直接令返回值减去数组首地址即可。
#include<iostream>
#include<algorithm>
using namespace std;
int main(){int a[10]={1,2,2,3,3,3,5,5,5,5};//寻找3cout<<lower_bound(a,a+10,3)-a<<","<<upper_bound(a,a+10,3)-a<<endl;return 0;
}
输出结果:
3,6
二分搜索:lower_bound 与 upper_bound 函数相关推荐
- C++ lower_bound 与 upper_bound 函数
头文件: #include <algorithm> 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound lower_bound(起始地址,结束地址 ...
- lower_bound()和upper_bound( )函数详解
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的.两个函数的用法类似,在一个左闭右开的有序区间里进行二分查找,需要查找的值由第三个参数给出 ...
- C++ lower_bound() upper_bound() 函数用法详解(深入了解,一文学会)
find().find_if().search() 等.值得一提的是,这些函数的底层实现都采用的是顺序查找(逐个遍历)的方式,在某些场景中的执行效率并不高.例如,当指定区域内的数据处于有序状态时,如果 ...
- C++/C--二分查找之lower_bound( )和upper_bound( )【转载】
文章目录 1 头文件及相关函数 2 函数lower_bound() 3 函数upper_bound() 4 示例 1 头文件及相关函数 头文件: #include <algorithm> ...
- upperbound找不到_关于lower_bound( )和upper_bound( )的常见用法
头文件:#include lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begi ...
- lower_bound()与upper_bound()
lower_bound和upper_bound函数用法 一,在数组中使用lower_bound()upper_bound() 二.STL中的lower_bound()和upper_bound() lo ...
- lower_bound()和upper_bound()的简单讲解
前几天复习二分然后百度了下lower_bound()和upper_bound()函数,现在将我的理解说下,先贴出我写的代码! #include<iostream> using namesp ...
- lower_bound( )和upper_bound( )的基本用法
lower_bound( )和upper_bound( )是C++ STL模板库中的函数,其作用可以看作是一个简单的二分模板. 基本使用方法: lower_bound: 参数:lower_bound( ...
- 二分检索函数lower_bound()和upper_bound()
二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower ...
最新文章
- linux fork 目录,linux fork()理解
- python中a=a+2与a+=2的区别
- “拯救网站运维经理赵明”有奖方案征集启事
- 用递归与分治策略求解网球循环赛日程表_算法设计:分治法(比赛日程安排)...
- csds原装的Markdown文档
- java上传文件堆溢出_我的正则表达式导致Java中的堆栈溢出;我错过了什么?
- 初学者python笔记(装饰器、高阶函数、闭包)
- Apache JMeter (二)性能测试 入门实例
- 判断numpy的array中是否包含nan,NaN
- 如何更改webstrom的默认端口63342
- spark Transformations算子
- 在udp聊天器里如何给飞秋发消息
- 如何下载B站视频(方法持续更新)
- 如何用个人电脑打造量子模拟器
- 姿态估计mmpose一手体验 Ⅱ - 使用它!
- LOL手游登上去服务器维护,lol手游进不去怎么回事?无法登陆解决方法
- Generic XDP Hook
- python 的 *args和 **kwargs 分别是什么意思
- Wav ,flac,mp3,ogg 等的区别
- python设计模式篇3---创建型模式