一、背景
lower_bound()和upper_bound(),binary_search()都是利用二分查找的方法在一个排好序的数组中进行查找的。
lower_bound():返回大于或等于目标值的第一个位置
upper_bound():返回大于目标值的第一个位置
binary_search():若目标值存在则返回true,否则返回false
二、lower_bound( )
lower_bound 的作用是在已经排好序的数组中返回第一个大于等于(不小于)你所给定的值。
源码版本一
template<class ForwardIt, class T>
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value)
{
ForwardIt it;
typename std::iterator_traits<ForwardIt>::difference_type count, step;
count = std::distance(first, last); // 首项与尾项的距离
while (count > 0) {
it = first;
step = count / 2;
std::advance(it, step); // 将 it 迭代器右移 n 个位置。
if (*it < value) {
first = ++it;
count -= step + 1;
}
else
count = step;
}
return first;
}
源码版本二
template<class ForwardIt, class T, class Compare>
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value, Compare comp)
{
ForwardIt it;
typename std::iterator_traits<ForwardIt>::difference_type count, step;
count = std::distance(first, last);
while (count > 0) {
it = first;
step = count / 2;
std::advance(it, step);
if (comp(*it, value)) {
first = ++it;
count -= step + 1;
}
else
count = step;
}
return first;
}
三、upper_bound( )
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。
源码版本一
template<class ForwardIt, class T>
ForwardIt upper_bound(ForwardIt first, ForwardIt last, const T& value)
{
ForwardIt it;
typename std::iterator_traits<ForwardIt>::difference_type count, step;
count = std::distance(first, last);
while (count > 0) {
it = first;
step = count / 2;
std::advance(it, step);
if (!(value < *it)) {
first = ++it;
count -= step + 1;
}
else
count = step;
}
return first;
}
源码版本二
template<class ForwardIt, class T, class Compare>
ForwardIt upper_bound(ForwardIt first, ForwardIt last, const T& value, Compare comp)
{
ForwardIt it;
typename std::iterator_traits<ForwardIt>::difference_type count, step;
count = std::distance(first, last);
while (count > 0) {
it = first;
step = count / 2;
std::advance(it, step);
if (!comp(value, *it)) {
first = ++it;
count -= step + 1;
}
else
count = step;
}
return first;
}
四、 binary_search()
在数组中以二分法检索的方式查找,若在数组中查找到indx元素则真,若查找不到则返回值是假
源码版本
template <class ForwardIterator, class T>
bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)
{
first = std::lower_bound(first,last,val);
return (first!=last && !(val<*first));
}
五、实例
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
const std::vector<int> data = { 1, 2, 4, 5, 5, 6 };
for (int i = 0; i < 8; ++i) {
// 返回数组中第一个大于或等于i的值
auto lower = std::lower_bound(data.begin(), data.end(), i);
std::cout << i << " ≤ ";
lower != data.end()
? std::cout << *lower << " at index " << std::distance(data.begin(), lower)
: std::cout << "not found";
std::cout << '\n';
}
std::cout << '\n';
// 返回数组中第一个大于5的值
auto upper = std::upper_bound(data.begin(), data.end(), 5);
upper != data.end()
? std::cout << *upper << " at index " << std::distance(data.begin(), upper)
: std::cout << "not found";
std::cout << '\n';
int a[100] = { 5,9,11,30,69,70,96,100 };
bool ans = binary_search(a, a + 9, 5);//查找成功,返回true
cout << "在数组中查找元素5,结果为:" << std::boolalpha << ans << endl;
bool res = binary_search(a, a + 9, 99);//查找失败,返回false
cout << "在数组中查找元素99,结果为:" << std::boolalpha << res << endl;
system("pause");
return 0;
}
打印
参考:
std::lower_bound - cppreference.com
std::upper_bound - cppreference.com
upper_bound和lower_bound的区别_zifengningyu的博客-CSDN博客_upper_bound