研究了一下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;
}