C++ STL中的Binary search(二分查找)

本文详细介绍了C++标准模板库(STL)中的二分查找(binary_search),lower_bound及upper_bound函数的使用方法和原理。包括如何在有序数组中查找特定元素,以及查找大于或等于、大于指定值的元素位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++ STL中的Binary search(二分查找)

**二分查找适合有一定序列的数列**
二分查找适合有一定序列的数列

  1. 头文件:
  #include<algorithm>
  1. 使用方法:
    a. 函数模板: binary_search(arr[],arr[]+size,indx)
    b. 参数说明:
    arr[] :数组首地址
    size:数组元素的个数
    indx:需要查找的值
    c:函数功能:在数组中以二分法检索的方式查找,若在数组(要求数组元素非递减)中查找到indx元素则真,若查找不到则返回值为假。
    查找成功,返回1;用法示例:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[100]= {4,10,11,30,69,70,96,100};
    int b=binary_search(a,a+9,11);//查找成功,返回1
    cout<<b<<endl;
    int c=binary_search(a,a+9,40);//查找失败,返回0
    cout<<c<<endl;
    int d=lower_bound(a,a+9,10)-a;
    cout<<d<<endl;
    int e=lower_bound(a,a+9,101)-a;
    cout<<e<<endl;
    int f=upper_bound(a,a+9,10)-a;
    cout<<f<<endl;
    int g=upper_bound(a,a+9,101)-a;
    cout<<g<<endl;
}

输出结果:

1
0
1
9
2
9

Process returned 0 (0x0)   execution time : 0.097 s
Press any key to continue.

2.lower_bound:查找第一个大于或等于某个元素的位置。
a.函数模板:lower_bound(arr[],arr[]+size , indx):
b.参数说明:
arr[]: 数组首地址
size:数组元素个数
indx:需要查找的值
c.函数功能: 函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置(注意是地址)。如果所有元素都小于val,则返回last的位置
d.举例如下:
  一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标,则
  /注意因为返回值是一个指针,所以减去数组的指针就是int变量了/
  pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。
  pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。
  pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。
e.注意:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!

返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置

3.upper_bound:查找第一个大于某个元素的位置。
a.函数模板:upper_bound(arr[],arr[]+size , indx):
b.参数说明:
arr[]: 数组首地址
size:数组元素个数
indx:需要查找的值
c.函数功能:函数upper_bound()返回的在前闭后开区间查找的关键字的上界,返回大于val的第一个元素位置
  例如:一个数组number序列1,2,2,4.upper_bound(2)后,返回的位置是3(下标)也就是4所在的位置,同样,如果插入元素大于数组中全部元素,返回的是last。(注意:数组下标越界)
  返回查找元素的最后一个可安插位置,也就是“元素值>查找值”的第一个元素的位置 。

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[100]= {4,10,11,30,69,70,96,100};
    int b=binary_search(a,a+9,11);//查找成功,返回1
    cout<<b<<endl;
    int c=binary_search(a,a+9,40);//查找失败,返回0
    cout<<c<<endl;
    int d=lower_bound(a,a+9,10)-a;
    cout<<d<<endl;
    int e=lower_bound(a,a+9,69)-a;
    cout<<e<<endl;
    int f=upper_bound(a,a+9,10)-a;
    cout<<f<<endl;
    int g=upper_bound(a,a+9,69)-a;
    cout<<g<<endl;
}

输出结果:

1
0
1
4
2
5

Process returned 0 (0x0)   execution time : 0.071 s
Press any key to continue.


二分搜索的代码:

#include <cstdio>
#include <algorithm>
#include<iostream>
using namespace std;
int main()
{
    int a[10]= {4,10,11,30,69,70,96,100};
    int left=0,mid,right=10-1,i,key=30;  //查找30;
    while(left<=right)
    {
        mid=(left+right)/2;
        if(a[mid]==key)
        {
            cout<<mid<<endl;
            break;
        }
        else if(a[mid]>key)
             {
                 right=mid-1;
             }
             else
             {
                 left=mid+1;
             }
    }
    return 0;
}

运行结果:

3

Process returned 0 (0x0)   execution time : 0.078 s
Press any key to continue.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Liknana

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

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

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

打赏作者

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

抵扣说明:

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

余额充值