二分检索函数lower_bound()和upper_bound()

二分检索函数lower_bound()和upper_bound()

一、说明

头文件:<algorithm>

二分检索函数lower_bound()和upper_bound()
lower_bound():找到大于等于某值的第一次出现
upper_bound():找到大于某值的第一次出现
必须从小到大排序后才能用
内部查找方式为二分查找,二分查找必定需要排序

返回值为地址

二、代码及结果

 1 /*
 2 二分检索函数lower_bound()和upper_bound()
 3 lower_bound():找到大于等于某值的第一次出现
 4 upper_bound():找到大于某值的第一次出现
 5 必须从小到大排序后才能用
 6 内部查找方式为二分查找,二分查找必定需要排序
 7 返回值为地址
 8 */
 9 #include <iostream>
10 #include <algorithm>
11 #include <string>
12 using namespace std;
13
14
15 int main(){
16
17     int a[]={9,2,4,5,10,7,30};
18     sort(a,a+7);//省略掉排序规则的形式,默认从小到大
19     //sort(a,a+7,less<int>());//用系统的排序规则,从小到大
20     //sort(a,a+7,greater<int>());//用系统的排序规则,从大到小
21     for(int i=0;i<7;i++){
22         cout<<a[i]<<" "<<&a[i]<<endl;
23     }
24     cout<<endl;
25     /*
26     这个lower_bound要从小到大排序才正确,
27     如果是从大到小,或者不排序,还是输出的从小到大排序好后的位置
28     */
29     int *p=lower_bound(a,a+7,2);//用lower_bound找大于等于2的第一次出现
30     cout<<p<<endl;
31     cout<<*p<<endl;
32     int *p1=upper_bound(a,a+7,2);//用upper_bound找大于等于2的第一次出现
33     cout<<p1<<endl;
34     cout<<*p1<<endl;
35
36
37
38     return 0;
39 } 

时间: 2024-08-06 01:06:38

二分检索函数lower_bound()和upper_bound()的相关文章

二分查找确定lower_bound和upper_bound

lower_bound当target存在时, 返回它出现的第一个位置,如果不存在,则返回这样一个下标i:在此处插入target后,序列仍然有序. 代码如下: int lower_bound(int* nums, int numsSize, int target) { //注意left和right的初始值必须是left = 0, right = numsSzie, 因为插入的位置可能是[0,numsSize] int left = 0; int right = numsSize; int mid;

STL函数 lower_bound 和 upper_bound 在算法竞赛中的用法

以前比较排斥这两个函数,遇到二分都是手写 \(while(left<=right)\). 这次决定洗心革面记录一下这两个函数的在算法竞赛中的用法,毕竟一般不会导致TLE. 其实百度百科已经概述得比较清楚了, 我们假设 \(value\) 为一个给定的数值, \(lower\_bound\) 是在一个升序序列中从前后后找第一个大于等于 \(value\) 的值, \(upper\_bound\) 是在一个升序序列中从前后后找第一个大于 \(value\) 的值. 比如:\(lower\_bound

STL中的二分查找——lower_bound 、upper_bound 、binary_search

STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中,进行二分查找查找某一元素val,函数lower_bound()返回大于或等于val的第一个元素位置(即满足条件a[i]>=val(first<=i<last)的最小的i的值),当区间的所有元素都小于val时,函数返回的i值为last(注意:此时i的值是越界的!!!!!). 比如:已知数组元素是a[10]={0,2,2,2,6,8,10,16,60,100} 当val=0时,函

C++STL中lower_bound() 和 upper_bound()二分查找

lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 通常用sort函数从小到大排序. 在从小到大的排序数组中, lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end.通过返回的地址减去起始地址begin,得到找到数字在数组中的下标. upper_bound( begin,end,num):从数组的begin位置到en

二分lower_bound()与upper_bound()的运用

<span style="color:#6633ff;">/* G - 二分 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Status Description Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each o

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

由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊的函数,lower_bound.upper_bound.equal_range. 原型如下: iterator lower_bound (const value_type& val) const; iterator upper_bound (const value_type& val) con

【C++】【STL】二分查找函数

binary_search 这个函数的返回值是布尔型,也就是最简单的找到了就为真,没找到就是假. 传入参数有三个,数据集合的左端点,数据集合的右端点,查找的值. 注意这些左端点右端点是要求左开右闭原则的,就是和数学上的左开右闭区间[a, b)一样,右端点是个不会被查阅的值. 一般来说写法类似: bool flag = false; int data[n] = {...};///数据 sort(data, data + n); flag = binary_search(data, data + n

[转] STL源码学习----lower_bound和upper_bound算法

http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < value 或者第一个 = value的位置 upper_bound of value 就是第一个 > value的位置 lower_bound的意思是一段相等的序列的头(闭)和尾(开)的位置 STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search

STL二分查找函数的应用

应用二分查找的条件必须是数组有序! 其中二分查找函数有三个binary_serch,upper_bound,lower_bound 测试数组 int n1[]={1,2,2,3,3,4,5}; int n2[]={5,4,3,3,2,2,1}; binary_serch 没有什么好说的,这个很简单,接受三个参数first,last,key三个值.如果在数组中查询到的话,那么就返回1否则返回0 代码 if(binary_search(n1,n1+7,3)) cout<<1<<&quo