C++二分查找:lower_bound( )和upper_bound( )
#include<algorithm>//头文件
//标准形式
lower_bound(int* first,int* last,val);
upper_bound(int* first,int* last,val);
lower_bound( )和upper_bound( )都是利用二分查找在一个排好序的从小到大的数组中进行查找,时间复杂度为O(logn)。
函数lower_bound()在first和last中的前闭后开区间,进行二分查找。返回从first开始的第一个大于或等于val的元素的地址。如果所有元素都小于val,则返回last的地址。
函数upper_bound()在first和last中的前闭后开区间,进行二分查找。返回从first开始的第一个大于val的元素的地址。如果所有元素都小于或等于val,则返回last的地址。
通常用法:
T* FoundAddr = std::lower_bound(&pBegin[], &pBegin[Count], Value);
若数组排序是从大到小,可以通过重载lower_bound( )和upper_bound( )函数实现功能。
完毕!
有任何问题欢迎留言讨论~
C++二分查找:lower_bound( )和upper_bound( )的更多相关文章
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- Long Jumps(二分查找lower_bound()函数的运用)
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long ju ...
- HDU 5178:pairs(二分,lower_bound和upper_bound)
pairs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 线段树离散化 unique + 二分查找 模板 (转载)
离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率. 通俗的说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小.例如: 原数据:1,999,100000,15:处理 ...
- 徒手实现lower_bound和upper_bound
STL中lower_bound和upper_bound的使用方法:STL 二分查找 lower_bound: ; ; //初始化 l ,为第一个合法地址 ; //初始化 r , 地址的结束地址 int ...
- 分治算法(二分查找)、STL函数库的应用第五弹——二分函数
分治算法:二分查找!昨天刚说不写算法了,但是突然想起来没写过分治算法的博客,所以强迫症的我…… STL函数库第五弹——二分函数lower_bound().upper_bound().binary_se ...
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...
- 二分查找法(binary_search,lower_bound,upper_bound,equal_range)
binary_search(二分查找) //版本一:调用operator<进行比较 template <class ForwardIterator,class StrictWeaklyCo ...
随机推荐
- 面试京东T5,被按在地上摩擦,鬼知道我经历了什么?
一转眼间,光阴飞快,各大企业都开始招人,各大学校也开始准备陆续入学. 金三银四已经快结束了,有的朋友发来喜报,面试上了一线大厂,也有没有面试上的朋友跑来跟我说,被虐惨了,几天给大家分享下我一个面试京东 ...
- java 两个数组相减结果
public static void main(String[] args) { String[] a = new String[] { "1", "5", & ...
- IDEA 导入maven 项目,出现报错,不能运行之类的
选择pom.xml,右键选择 Add as Maven Project
- k8s教程:Kubernetes集群使用网络存储NFS
NFS存储 NFS即网络文件系统Network File System,它是一种分布式文件系统协议,最初是由Sun MicroSystems公司开发的类Unix操作系统之上的一款经典网络存储方案,其功 ...
- 关于IDEA的一些快捷键操作
shift+F6修改实体类中的属性会重构代码
- Win10系统报错问题集锦
收集记录win10的坑 错误1 应用程序-特定 权限设置并未向在应用程序容器 不可用 SID (不可用)中运行的地址 LocalHost (使用 LRPC) 中的用户 NT AUTHORITY\SYS ...
- 2020年最新.NET面试题
.net 面试题最新版 2020-2-26 每日几道面试题1. .NET和C#有什么区别答:.NET一般指 .NET FrameWork框架,它是一种平台,一种技术.C#是一种编程语言,可以基于. ...
- time strptime()方法 时间操作
Python time strptime()方法 时间操作 描述 Python time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组. 语法 strptime()方法语 ...
- Spring集成Quartz定时任务
1.导入jar包 2.配置applicationContext.xml文件 <!-- 任务调度1 --> <!-- bean id="simpleJob" cla ...
- Mybatis-Plus中Wrapper的方法
public interface EntityService extends IService<TbEntity>{ }entityService.update(entity,Condit ...