C++ lower_bound()与upper_bound()

头文件: #include  <algorithm>

 

二分查找的函数有 3 个:

1.lower_bound(起始地址,结束地址,要查找的数值) 返回的是数值第一次出现的位置。

2.upper_bound(起始地址,结束地址,要查找的数值) 返回的是数值最后一次出现之后的位置。

3.binary_search(起始地址,结束地址,要查找的数值)  返回的是是否存在这么一个数,是一个bool值。

 

Attention:既然原理是二分查找,那么就需要保证查找的数组是有序的!

 

lower_bound():

功能:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置.

注意:如果所有元素都小于val,则返回last的位置,且last的位置是越界的!

 

upper_bound():

功能:函数upper_bound()返回的在前闭后开区间查找的关键字的上界,返回大于val的第一个元素位置

注意:返回查找元素的最后一个可安插位置,也就是“元素值>查找值”的第一个元素的位置。同样,如果val大于数组中全部元素,返回的是last。(注意:数组下标越界)

#include<bits/stdc++.h>

using namespace std;

int main(){
	int a[8] = {1, 3, 3, 7, 8, 8, 8, 9};
	int is_exist = binary_search(a, a + 8, 3);
	int lower_index = lower_bound(a, a + 8, 3) - a;
	//if a is a vector, not array, we can write like this:
	//lower_bound(a.begin(), a.end(), 3) - a.begin();
	int upper_index = upper_bound(a, a + 8, 3) - a;
	cout << "is_exist: " << is_exist << endl;
	cout << "lower_index: " << lower_index << endl;
	cout << "upper_index: " << upper_index << endl;
    return 0;
} 
is_exist: 1
lower_index: 1
upper_index: 3

--------------------------------
Process exited after 0.3126 seconds with return value 0
请按任意键继续. . .
#include<bits/stdc++.h>

using namespace std;

int main(){
	int a[8] = {1, 3, 3, 7, 8, 8, 8, 9};
	int is_exist = binary_search(a, a + 8, 5);
	int lower_index = lower_bound(a, a + 8, 5) - a;
	int upper_index = upper_bound(a, a + 8, 5) - a;
	cout << "is_exist: " << is_exist << endl;
	cout << "lower_index: " << lower_index << endl;
	cout << "upper_index: " << upper_index << endl;
    return 0;
} 
is_exist: 0
lower_index: 3
upper_index: 3

--------------------------------
Process exited after 0.6841 seconds with return value 0
请按任意键继续. . .

可以看出,前面的数组是从小到大的,如果是从大到小,就需要重载了:

lower_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

#include<bits/stdc++.h>

using namespace std;

int main(){
	int a[8] = {9, 8, 8, 8, 7, 3, 3, 1};
	int is_exist = binary_search(a, a + 8, 8);
	int lower_index = lower_bound(a, a + 8, 8, greater<int>()) - a;
	int upper_index = upper_bound(a, a + 8, 8, greater<int>()) - a;
	cout << "is_exist: " << is_exist << endl;
	cout << "lower_index: " << lower_index << endl;
	cout << "upper_index: " << upper_index << endl;
    return 0;
} 
is_exist: 0
lower_index: 1
upper_index: 4

--------------------------------
Process exited after 0.6533 seconds with return value 0
请按任意键继续. . .
#include<bits/stdc++.h>

using namespace std;

int main(){
	int a[8] = {9, 8, 8, 8, 7, 3, 3, 1};
	int is_exist = binary_search(a, a + 8, 6);
	int lower_index = lower_bound(a, a + 8, 6, greater<int>()) - a;
	int upper_index = upper_bound(a, a + 8, 6, greater<int>()) - a;
	cout << "is_exist: " << is_exist << endl;
	cout << "lower_index: " << lower_index << endl;
	cout << "upper_index: " << upper_index << endl;
    return 0;
} 
is_exist: 0
lower_index: 5
upper_index: 5

--------------------------------
Process exited after 0.1253 seconds with return value 0
请按任意键继续. . .

参考:

https://www.cnblogs.com/Tang-tangt/p/9291018.html

https://blog.csdn.net/qq_40160605/article/details/80150252

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值