STL中非常好用的二分查找函数汇总

1、搜索某个元素是否存在,使用binary_search(beg,end,val)或binary_search(beg,end,val,op)

2、获得被搜寻元素的位置,应使用lower_bound(),upper_bound()或equal_range()参数与以上相同

3、检查若干个值是否存在bool includes(beg,end,searchBeg,searchEnd)或bool includes(beg,end,searchBeg,searchEnd,op)

需要注意:

1、以上函数的使用必须都只适用于已排序的序列

2、op是一个可有可无的二元判断式,被用来作为排序准则:op(elem1,elem2)

接下来看一下这几个函数的使用

1、binary_search(beg,end,val)

返回一个bool变量,以二分法检索的方式在[beg,end]之间查找val,找到返回true,找不到返回false。

2、lower_bound(beg,end,val)

返回一个迭代器,指向非递减序列[first, last)中的第一个大于等于(>=)val的位置。 
3、upper_bound(beg,end,val)

返回一个迭代器,指向非递减序列[first, last)中的第一个大于 (>) val的位置。

注意:对于lower_bound & upper_bound()还有一个第四个参数greater<type>(),他会把不小于(>=)变为不大于(<=)得查找,把大于(>)变为小于(<)查找,因此使用时要求序列不是递增,而是递减!!!此外,还必须加头文件#include <functional>

4、equal_range(beg,end,val)

返回一个迭代器对(i,j),其中i是在不破坏次序的前提下,value可插入的第一个位置(>=亦即lower_bound),j则是在不破坏次序的前提下,value可插入的最后一个位置(>亦即upper_bound)因此,[i,j)内的每个元素都等同于value,而且[i,j)是[beg,end)之中符合此一性质的最大子区间

注:以上函数的复杂度,如果搭配随机存取迭代器,则为对数复杂度,否则为线性复杂度;

5、bool includes(beg,end,searchBeg,searchEnd)

bool includes(beg,end,searchBeg,searchEnd,op)

返回一个bool变量,两种形式都用来判断有序序列[beg,end)是否包含另一个有序序列[searchBeg,searchEnd)的全部元素,也就是说对于[searchBeg,searchEnd)中的每一个元素,如果[beg,end)必有一个对应的相等元素,那么[searchBeg,searchEnd)肯定是[beg,end)的子集。

复杂度:线性

接下来给出这几种函数的使用代码示例:

#include<iostream>
#include<algorithm>
#include<vector>
#include<functional>
using namespace std;
int main()
{
    int a[]={4,10,11,30,30,69,70,96,100};
    int a2[]={30,69,70};
    int a3[]={30,50,70};
	vector<int> vec(a,a+9);
	vector<int> vec2(a2,a2+3);
	vector<int> vec3(a3,a3+3);
    int binary1=binary_search(vec.begin(),vec.end(),4);
    cout<<"在数组中查找元素4,结果为:"<<binary1<<endl;//查找成功,返回1

    int binary2=binary_search(vec.begin(),vec.end(),40);
    cout<<"在数组中查找元素40,结果为:"<<binary2<<endl;//查找失败,返回0

    int lower1=lower_bound(vec.begin(),vec.end(),10)-vec.begin();
    cout<<"在数组中查找第一个大于等于10的元素位置,结果为:"<<lower1<<endl;//返回1 

    int lower2=lower_bound(vec.begin(),vec.end(),101)-vec.begin();
    cout<<"在数组中查找第一个大于等于101的元素位置,结果为:"<<lower2<<endl;//返回9 

    int upper1=upper_bound(vec.begin(),vec.end(),10)-vec.begin();
    cout<<"在数组中查找第一个大于10的元素位置,结果为:"<<upper1<<endl;//返回2 

    int upper2=upper_bound(vec.begin(),vec.end(),101)-vec.begin();
    cout<<"在数组中查找第一个大于101的元素位置,结果为:"<<upper2<<endl;//返回9 

    auto bounds=equal_range(vec.begin(),vec.end(),30);
    int min=bounds.first-vec.begin();
    int max=bounds.second-vec.begin();
    cout<<"在数组中查找到的30所在的范围为"<<"["<<min<<","<<max<<")"<<endl;//返回[3,5)

    auto bounds2=equal_range(vec.begin(),vec.end(),100);
    min=bounds2.first-vec.begin();
    max=bounds2.second-vec.begin();
    cout<<"在数组中查找到的100所在的范围为"<<"["<<min<<","<<max<<")"<<endl;//返回[8,9)

    int include1=includes(vec.begin(),vec.end(),vec2.begin(),vec2.end());
    cout<<"在vec1中查找vec2,结果为:"<<include1<<endl; //查找成功,返回1 

    int include2=includes(vec.begin(),vec.end(),vec3.begin(),vec3.end());
    cout<<"在vec1中查找vec3,结果为:"<<include2<<endl; //查找失败,返回0 
}

其中也要注意一下代码中vector的初始化方法

想要输出下标的话用(迭代器-beg)就可以了

 

 

  • 14
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值