lower_bound,upper_bound的第四个参数的用法

转载自:https://blog.csdn.net/qq_35620616/article/details/70232489 

1.lower_bound的第四个参数的用法:

先看代码:

#include <bits/stdc++.h>
using namespace std;
struct node{
    int x,y;
};
struct cmp{
    int operator()(const node a,const node b){
        return a.x<b.x;
    }
};
int main(){
    node num[1000];
    for(int i=0;i<=100;i++){
        num[i].x=i;
        num[i].y=100-i;
        printf("%d ",num[i].x);
    }
    printf("\n");
    while(1){
        node t;
        scanf("%d",&t.x);
        printf("%d\n",num[(lower_bound(num,num+100,t,cmp())-num)].x);
 
    }
}
大致的模板就是这样,注意,引用cmp的时候是有()的。

下面来解释一下加入cmp()参数的lower_bound的运行过程

struct cmp{
    int operator()(const node a,const node b){
        return a.x<b.x;
    }
};
在这里,a代表的就是num数组的数字,而b代表的就是要比较的数字,如果cmp的retur是1的话那么二分查找就会往右边进行,而如果是0的话那么就会往左边进行。
我们还是举数据来解释吧


假设mid代表num区间的中间位置

比方说输入的是个68,那么首先68会和50(二分查找嘛,自然是从num数组的中间开始进行比较的拉)进行比较因为50<68为1,那么mid就会往右跑了,接下来因为75<68为零所以mid又会往左跑,接下来就是63<68然后mid又往右跑,直到逼近到<=68的数字中最接近68的数字

假设cmp是这样的就比较神奇了:

struct cmp{
    int operator()(const node a,const node b){
        return a.x>b.x;
    }
};
这次拿68和49举例
对于68而言50>68不满足,然后往左跑,然后越跑越不满足,于是就输出了0;

对于49而言50>49满足,然后往右跑,越跑越满足根本停不下来直到跑到最右端于是就输出了100......

注意:如果这是一个递减的序列的话那么cmp也要发生变化,具体怎么变参照上面的例子,(主要是因为太长不想写.......哈哈哈.......)

对于upper_bound就比较神奇了,它是这么利用cmp的:

struct cmp{
    int operator()(const node a,const node b){
        return a.x<b.x;
    }
};

a代表要比较的数字,b代表num数组,如果return为1就往左跑,否则往右跑。(我会告诉你之所以我这么认为仅仅是因为这符合我的经验法则吗)
我们还是举例吧,比如num序列是这样的:

11223344

12345678(这一行代表上面那些数字对应的下标)

当用lower_bound来查找2应该插入哪个位置的时候是这样的:首先num[4]与2比较,num[4]<2是false的,所以往左跑;然后num[2]<2是true的,往右跑;直到逼近到num[3]这个位置

当用upper_bound来查找2应该插入哪个位置的时候是这样的:首先2与num[4]比较。2<num[4]是false的,所以往右跑,然后2<num[6]是true的,往左跑,直到逼近到num[5]这个位置;


具体的cmp形参的变化过程看官可以用以下代码测试以下:

#include <bits/stdc++.h>
using namespace std;
struct node{
    int x,y;
};
struct cmp{
    int operator()(const node a,const node b){
        printf("*%d %d\n",a.x,b.x);
        return a.x<b.x;
    }
};
int main(){
    node num[1000];
    for(int i=0;i<=100;i++){
        num[i].x=i/10;
        num[i].y=100-i;
        printf("%d ",num[i].x);
    }
    printf("\n");
    while(1){
        node t;
        scanf("%d",&t.x);
        printf("%d\n",num[(lower_bound(num,num+100,t,cmp())-num)].x);
        printf("%d\n\n",num[(upper_bound(num,num+100,t,cmp())-num)].x);
 
    }
}

另外,set和map,和map容器也有直接的内置二分查找,这样使用:

set<int>s;

s.upper_bound()或者s.lower_bound(这里填参数);

注意:

对于map<first_int,second_int>,他的lower_bound实在first_int里面查找你填入的参数的:比如这样

#include <bits/stdc++.h>
#define LL long long
#define inf 1e9+1
using namespace std;
int main(){
    map<int ,int>m;
    for(int i=1;i<=5;i+=2)m[i]=i+4;
    printf("%d\n",*m.lower_bound(2));
}


特别声明!特别声明!
本人不保证上面内容确实是lower_bound和upper_bound真正的实现方法,只是在本人的一些测试中上面的这些能够正常解释我的所有样例。
如果有路过dalao发现任何错误还望提出,以免误人子弟。谢谢!
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
lower_boundupper_bound是C++中的函数,用于在有序数组中查找特定元素的位置。 lower_bound函数返回大于等于给定值的第一个元素的位置,而upper_bound函数返回大于给定值的第一个元素的位置。 在使用lower_boundupper_bound函数之前,需要确保数组已经按升序排序。这两个函数的使用方法如下: ```cpp int* lower_bound(int* first, int* last, const int& val); int* upper_bound(int* first, int* last, const int& val); ``` 其中,`first`和`last`指定了要查找的数组的范围,`val`是要查找的值。函数返回的是一个指针,指向数组中的位置。 下面是一个示例代码,展示了lower_boundupper_bound函数的使用: ```cpp #include <iostream> #include <algorithm> using namespace std; int main() { const int n = 6; int arr[n]{0, 1, 5, 8, 10, 11}; int* i_lower = lower_bound(arr, arr + n, 8); int* i_upper = upper_bound(arr, arr + n, 8); cout << "i_lower: " << i_lower - arr << endl; // i_lower: 3 cout << "i_upper: " << i_upper - arr << endl; // i_upper: 4 int* j_lower = lower_bound(arr, arr + n, 10); int* j_upper = upper_bound(arr, arr + n, 10); cout << "j_lower: " << j_lower - arr << endl; // j_lower: 4 cout << "j_upper: " << j_upper - arr << endl; // j_upper: 5 return 0; } ``` 在这个例子中,我们定义了一个有序数组arr,并使用lower_boundupper_bound函数查找了值为8和10的元素的位置。输出结果表明,8在数组中的位置范围是<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [lower_bound()与upper_bound()](https://blog.csdn.net/Boruson/article/details/125702597)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值