其实这两个函数只能用于 “升序” 序列。
#include<bits/stdc++.h>
using namespace std;
int main() {
int a[] = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
cout << (lower_bound(a, a + 12, 4) - a) << endl; //输出 9
cout << (upper_bound(a, a + 12, 4) - a) << endl; //输出 12
cout << (lower_bound(a, a + 12, 1) - a) << endl; //输出 0
cout << (upper_bound(a, a + 12, 1) - a) << endl; //输出 3
cout << (lower_bound(a, a + 12, 3) - a) << endl; //输出 6
cout << (upper_bound(a, a + 12, 3) - a) << endl; //输出 9
cout << (lower_bound(a, a + 12, 5) - a) << endl; //输出 12
cout << (upper_bound(a, a + 12, 5) - a) << endl; //输出 12
cout << (lower_bound(a, a + 12, 0) - a) << endl; //输出 0
cout << (upper_bound(a, a + 12, 0) - a) << endl; //输出 0
return 0;
}
那么如果是降序序列呢?如果是降序序列,这个函数仍然以为你这个序列是升序的,
我们以{4,4,4,3,3,3,2,2,2,1,1,1}作为例子,试验一下。
#include<bits/stdc++.h>
using namespace std;
int main() {
int a[] = {4, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1};
cout << (lower_bound(a, a + 12, 4) - a) << endl; // 输出 12
cout << (upper_bound(a, a + 12, 4) - a) << endl; // 输出 12
cout << (lower_bound(a, a + 12, 1) - a) << endl; // 输出 0
cout << (upper_bound(a, a + 12, 1) - a) << endl; // 输出 0
cout << (lower_bound(a, a + 12, 3) - a) << endl; // 输出 12
cout << (upper_bound(a, a + 12, 3) - a) << endl; // 输出 12
return 0;
}
以这句为例 lower_bound(a, a + 12, 4) ,因为是二分查找,第一步从中间开始,取中间值 a[(0+12)/2] = a[6] = 2 ,比 4 小,但是他想要找到第一个大于等于 4 的,所以继续向更大的值靠近,向哪边靠近呢,右边,因为他以为你这是升序的,所以取右半部分中间值 a[(7+12)/2] = a[9] = 1,比 4 小,所以继续向更大的值靠近,取右半部分中间值 a[(10+12)/2] = a[11] = 1,比 4 小,所以继续向更大的值靠近,取右半部分中间值 a[(12+12)/2] = a[12],到这不用取了,到头了,该返回了,所以最终返回了尾迭代器。
简单来讲就是:
lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置。
upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中第一个大于val的位置。
首先是我修改数据结构课本上的二分查找实现的lower_bound算法:
int my_lower_bound(int *array, int size, int key)
{
int first = 0, last = size-1;
int middle, pos=0; //需要用pos记录第一个大于等于key的元素位置
while(first < last)
{
middle = (first+last)/2;
if(array[middle] < key){ //若中位数的值小于key的值,我们要在右边子序列中查找,这时候pos可能是右边子序列的第一个
first = middle + 1;
pos = first;
}
else{
last = middle; //若中位数的值大于等于key,我们要在左边子序列查找,但有可能middle处就是最终位置,所以我们不移动last,
pos = last; //而是让first不断逼近last。
}
}
return pos;
}
STL中源代码:
//这个算法中,first是最终要返回的位置
int lower_bound(int *array, int size, int key)
{
int first = 0, middle;
int half, len;
len = size;
while(len > 0) {
half = len >> 1;
middle = first + half;
if(array[middle] < key) {
first = middle + 1;
len = len-half-1; //在右边子序列中查找
}
else
len = half; //在左边子序列(包含middle)中查找
}
return first;
}
upper_bound返回的是最后一个大于等于val的位置,也是有一个新元素val进来时的插入位置。
我依然将二分查找略做修改:
int my_upper_bound(int *array, int size, int key)
{
int first = 0, last = size-1;
int middle, pos = 0;
while(first < last)
{
middle = (first+last)/2;
if(array[middle] > key){ //当中位数大于key时,last不动,让first不断逼近last
last = middle;
pos = last;
}
else{
first = middle + 1; //当中位数小于等于key时,将first递增,并记录新的位置
pos = first;
}
}
return pos;
}
STL中的upper_bound实现:
int upper_bound(int *array, int size, int key)
{
int first = 0, len = size-1;
int half, middle;
while(len > 0){
half = len >> 1;
middle = first + half;
if(array[middle] > key) //中位数大于key,在包含last的左半边序列中查找。
len = half;
else{
first = middle + 1; //中位数小于等于key,在右半边序列中查找。
len = len - half - 1;
}
}
return first;
}