Algorithm头文件--二分查找函数

在STL标准函数库中提供了一些查找函数,如:lower_bound( )、upper_bound( )和binary_search( )。它们的底层实现方式都是二分查找,需要注意的是,使用它们时都需要满足前提条件:即在一个已经排好序的数组中进行查找,且默认是非递减的数组,因为它们是查找第一个大于或等于查找数值的位置,所以需要容器处于有序的状态。

若数组未排序,我们可以用sort( )函数对数组进行排序(sort( )函数的具体用法可以参考:Algorithm头文件--sort()函数_PL_涵的博客-CSDN博客),若该数组是递减排序,我们可以使用reverse( )函数将数组逆序,或者传入自定义的查找方式。

使用函数查找的时间复杂度为O(log n)。

一、lower_bound()以及upper_bound()函数简介

1. 函数简介

两个函数的原型为:iterator lower_bound/upper_bound(start, end, num, find)

其中四个参数分别表示:

1)start表示查找开始的地址。

2)end表示查找结束的地址。

3)num表示查找的数值。

4)find为可选参数,表示查找方式,默认是对非降序数组进行查找。

函数返回值是一个迭代器,即一个指针。

2.函数用法

lower_bound( ):从容器的 start 位置到 end 位置的左闭右开区间内,使用二分查找第一个大于或等于 num 的数值。若找到,则返回该数值的地址;若不存在,则返回 end 。

一般的,我们可以通过返回的地址减去起始地址 start ,就可以得到数值在数组中的下标,或者是容器中小于 num 的数值的个数。

upper_bound( ):从容器的 start 位置到 end 位置的左闭右开区间内,二分查找第一个大于 num 的数值。若找到,则返回该数值的地址;若不存在,则返回 end 。

同样的,通过返回的地址减去起始地址 start ,就可以得到数值在数组中的下标,或者是容器中小于等于 num 的数值的个数。

前面,我们说到这两个函数默认是对非降序的数组进行查找,那么,如果数组是降序的,而我们又不想改变数组的顺序时该怎么办呢?这个时候,就需要用到第四个参数 find 了。

find参数与sort( ) 函数中的cmp参数一样的,实际上就是对lower_bound()和upper_bound()重载。

具体重载形式如下:

lower_bound( start, end, num, greater<type>( ) ):从容器的 start 位置到 end 位置的左闭右开区间内,二分查找第一个小于或等于 num 的数值,找到则返回该数值的地址;不存在则返回 end 。

upper_bound( start, end, num, greater<type>( ) ):从容器的 start 位置到 end 位置的左闭右开区间内,二分查找第一个小于 num 的数值,找到则返回该数值的地址;不存在则返回 end 。

#include<bits/stdc++.h>
using namespace std;
bool cmp(int a, int b){
	return a > b;
}
int main(){
	int num[] = {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;	
}

 实战例题:实战例题(第十一届蓝桥杯省赛真题-整数小拼接)_PL_涵的博客-CSDN博客

二、binary_search()函数简介

1. 函数简介

函数原型为:bool binary_search( start, end, num )

其中三个参数分别表示:

1)start表示查找开始的地址。

2)end表示查找结束的地址。

3)num表示查找的数值。

其返回值为bool类型。

2. 函数用法

binary_search( ):从容器的 start 位置到 end 位置的左闭右开区间内,二分查找 num ,找到则返回 true,不存在则返回 false。

#include<bits/stdc++.h>
using namespace std;
int main() {
	int a[] = {45, 31, 20, 12, 7};
	if(binary_search(a, a + 5, 7))
    cout << "查找成功!" << endl;
	//输出“查找成功!”
    return 0;
}
C++ STL实现二分查找函数有三个:lower_bound、upper_bound和binary_search。 lower_bound函数用来查找第一个不小于给定值的元素,如果存在多个相同的元素,则返回第一个出现的位置。 upper_bound函数用来查找第一个大于给定值的元素,如果存在多个相同的元素,则返回第一个大于给定值的位置。 binary_search函数用来判断给定值是否在有序容器中出现,如果存在则返回true,否则返回false。 在给出的代码中,使用了lower_bound函数来查找数组a中是否存在与b相等的元素,如果存在,则将其赋值给ans变量。 需要注意的是,使用这些函数前需要先对容器进行排序,以保证二分查找的正确性。可以使用algorithm头文件中的sort函数对容器进行排序。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [STL之二分查找](https://blog.csdn.net/jfkidear/article/details/100171041)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [C++ STL中的 二分查找](https://blog.csdn.net/shuaizhijun/article/details/88982556)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PL_涵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值