c++ lower_bound

 求下界的原理:

int lower_bound(int *A,int x,int y,int v){
    int m;
    while(x<y){
        m=x+(y-x)/2;
        if(A[m)>=v)    y=m;
        else x=m+1;
    }
    return x;
}

 lower_bound(start,end,val) 返回大于等与val的值

 数组中的用法:

#include <iostream>
#include<algorithm>
using namespace std;
int main(int argc, char const *argv[]) {
  int A[]={1,3,5,5,8,100,10301};
  int * resu=lower_bound(A,A+6,5);
  std::cout << A << '\n';
  std::cout << *resu << '\n';
  std::cout << *(resu+1) << '\n';
  std::cout << *(resu+2) << '\n';
  return 0;
}

返回的是第一数在数组中出现的首地址。

容器中的用法:

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(int argc, char const *argv[]) {
  vector<int>nums;
  nums.push_back(-242);
  nums.push_back(-1);
  nums.push_back(0);
  nums.push_back(5);
  nums.push_back(8);
  nums.push_back(8);
  nums.push_back(11);
  vector<int>::iterator result;
  int new_val=7;
  result=lower_bound(nums.begin(),nums.end(),new_val);
  std::cout << *result << '\n';
  new_val=8;
  result=lower_bound(nums.begin(),nums.end(),new_val);
  std::cout << *result << '\n';
  std::cout << *(result+1) << '\n';
  std::cout << *(result+2) << '\n';
  return 0;
}

 

 

自定义类

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class person
{
private:
    int age;//年龄
    double height;//身高
    string name;//姓名
public:
    person():age(0),height(0.0)
    {}
    person(const int age_,
            const double height_,
            const string &name_)
            :age(age_),
            height(height_),
            name(name_) {}
    int key1() const
    {
        return age;
    }
    double key2() const
    {
        return height;
    }
    const string &key3() const
    {
        return name;
    }
};
struct compareByTwoKey
{
    template<class T1,class T2>
    int operator()(const T1 &t1,const T2 &t2)
    {
        if(t1.key1()<t2.key1())
            return true;
        else
            if(t1.key1()==t2.key1())
            {
                if(t1.key2()<t2.key2())
                    return true;
                else
                    return false;
            }
            else
                return false;
    }
};
void init_data(vector<person> &v)
{
    v.push_back(person(12,96.8,"你好"));
    v.push_back(person(12,86.9,"你好"));
    v.push_back(person(13,76.8,"你好"));
    v.push_back(person(13,77.8,"你好"));
    v.push_back(person(10,70.8,"你好"));
    v.push_back(person(15,79.8,"你好"));
    v.push_back(person(18,176.8,"你好"));
    v.push_back(person(2,6.8,"你好"));
    v.push_back(person(12,55.8,"你好"));
    v.push_back(person(12,97.8,"hello"));
    sort(v.begin(),v.end(),compareByTwoKey());//一定要加上排序,并且排序函数跟lower_bound一致
}
int main()
{
    vector<person> v;
    init_data(v);
    personval(12,90.0,"hello");
    vector<person>::iteratoriter iter=lower_bound(v.begin(),v.end(),val,compareByTwoKey());//跟sort函数一致
    system("pause");
}

可以看到,lower_bound和sort一样。

求下标

int p=lower_bound(a,a+n,x)-a;//在排序好的数组里寻找x,返回x的index

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值