c++ lower_bound与upper_bound的比较器参数

研究了一下lower_bound,upper_bound的用法

lower_bound

我们知道,对于

lower_bound(a, a + 10, val)

是返回第一个满足a[i]>=val的位置
来看一下stl的源码

template <class ForwardIterator, class T>
  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
  ForwardIterator it;
  iterator_traits<ForwardIterator>::difference_type count, step;
  count = distance(first,last);
  while (count>0)
  {
    it = first; step=count/2; advance (it,step);
    if (*it<val) {                 // or: if (comp(*it,val)), for version (2)
      first=++it;
      count-=step+1;
    }
    else count=step;
  }
  return first;
}

显然,当a[it]<val时,二分的区间右移,即函数comp(*it,val)的返回值为1
那么我们可以通过该性质写出如下代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 11;

struct cmp
{
    bool operator()(const int a, const int b)
    {
        return a > b;
    }

};
int main()
{

    int a[10] = {8, 5, 4, 3, 2, 1, 0, -5, -19, -4366};
//在降序序列中,第一个小于等于i的值
     for (int i = -20; i <= 20; i++)
     cout << i << " " << *lower_bound(a, a + 10, i, cmp()) << endl;
    }

定义一个递减序列,我们重载了比较运算,即*it>val时,comp(*it,val)返回值为1,二分区间向右移,由此便可以实现找到第一个 a[i]<=val的位置
输出:
在这里插入图片描述
那么我们也可以仿照写出结构体的lower_bound:
需要注意的是,对结构体数组使用lower_bound传入的参数也应该是结构体

#include <bits/stdc++.h>
using namespace std;

const int maxn = 11;
struct node
{
    int x, y;
    bool operator<(const node m)const{
        return x>m.x;
    }

    node (int x,int y):x(x),y(y){}
    node(){}
} b[maxn];
struct cmp
{
    bool operator()(const int a, const int b)
    {
        return a > b;
    }

};

int main()
{

    int a[10] = {8, 5, 4, 3, 2, 1, 0, -5, -19, -4366};

    // for (int i = -50; i <= 50; i++)
        // cout << i << " " << *lower_bound(a, a + 10, i, cmp()) << endl;
    for (int i = 0; i < 10; i++)
    {
        b[i].x = a[i];
    } //在降序序列中,第一个小于等于i的值
    for (int i = -20; i <= 10; i++)
        cout << i << " " << (*lower_bound(b, b + 10, node(i,0))).x << endl;
}

upper_bound

我们知道,对于

upper_bound(a,a+n,val);

是在递增序列中,返回第一个满足a[i]>val的位置
来看一下stl的源码

template <class ForwardIterator, class T>
  ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
  ForwardIterator it;
  iterator_traits<ForwardIterator>::difference_type count, step;
  count = std::distance(first,last);
  while (count>0)
  {
    it = first; step=count/2; std::advance (it,step);
    if (!(val<*it))                 // or: if (!comp(val,*it)), for version (2)
      { first=++it; count-=step+1;  }
    else count=step;
  }
  return first;
}

当*it<=val时,区间右移,即函数comp(val,*it)的返回值为0

#include <bits/stdc++.h>
using namespace std;

const int maxn = 11;

struct cmp
{
    bool operator()(const int a, const int b)
    {
        return a > b;
    }

};

int main()
{

    int a[10] = {8, 5, 4, 3, 2, 1, 0, -5, -19, -4366};
//在降序序列中,第一个小于i的值
     for (int i = -20; i <= 10; i++)
     cout << i << " " << *upper_bound(a, a + 10, i, cmp()) << endl;
    }

对于一个递减序列,我们定义类cmp,当传入参数 val<=*it 时,返回值为0,区间右移,我们便可以找到第一个小于val的值
输出:
在这里插入图片描述
对结构体的使用:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 11;
struct node
{
    int x, y;
    bool operator<(const node m)const{
        return x>m.x;
    }

    node (int x,int y):x(x),y(y){}
    node(){}
} b[maxn];
struct cmp
{
    bool operator()(const int a, const int b)
    {
        return a > b;
    }

};

int main()
{

    int a[10] = {8, 5, 4, 3, 2, 1, 0, -5, -19, -4366};

    // for (int i = -50; i <= 50; i++)
        // cout << i << " " << *lower_bound(a, a + 10, i, cmp()) << endl;
    for (int i = 0; i < 10; i++)
    {
        b[i].x = a[i];
    } //在降序序列中,第一个小于i的值
   
    for (int i = -20; i <= 10; i++)
        cout << i << " " << (*upper_bound(b, b + 10, node(i,0))).x << endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值