c++(8):c++函数lower_bound(),upper_bound()

std::lower_bound()

1) 作用:

返回第一个不小于给定值的元素的迭代器

2) 链接:

c++官网: https://en.cppreference.com/w/cpp/algorithm/lower_bound
定义在头文件:

3) 定义:

定义的形式之二如下,其他形式差不多

(1)
template< class ForwardIt, class T >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );
(2)
template< class ForwardIt, class T, class Compare >
ForwardIt lower_bound( ForwardIt first, ForwardIt last,
                       const T& value, Compare comp );

在范围 [first, last) 中进行二分查找,查找不满足 element < value【或 comp(element, value))(即大于或等于)】的第一个元素的迭代器,如果未找到此类元素,则返回 last。

4) 参数:

first, last - 定义要检查的偏序范围的迭代器
value - 比较元素的值
comp - 如果第一个参数小于(即排在第二个之前),则返回 true 的二元谓词。

5) 返回值:

指向范围 [first, last) 中第一个元素的迭代器,使得 element < value【或 comp(element, value)】为 false,如果没有找到这样的元素,则为 last。

6)函数的算法:

第一版

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);
 
        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;
}

7)函数的案例:

#include <algorithm>
#include <iostream>
#include <vector>
 
struct PriceInfo { double price; };
 
int main()
{
    const std::vector<int> data = {1, 2, 4, 5, 5, 6};
 
    for (int i = 0; i < 8; ++i)
    {
        // Search for first element x such that i ≤ x
        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::vector<PriceInfo> prices = {
   
   {100.0}, {101.5}, {102.5}, {102.5}, {107.3}};
 
    for (double to_find : {102.5, 110.2})
    {
        auto prc_info = std::lower_bound(prices.begin(), prices.end(), to_find,
            [](const PriceInfo& info, double value)
            {
                return info.price < value;
            });
 
        prc_info != prices.end()
            ? std::cout << prc_info->price << " at index " << prc_info - prices.begin()
            : std::cout << to_find << " not found";
        std::cout << '\n';
    }
}

打印输出:

0 ≤ 1 at index 0
1 ≤ 1 at index 0
2 ≤ 2 at index 1
3 ≤ 4 at index 2
4 ≤ 4 at index 2
5 ≤ 5 at index 3
6 ≤ 6 at index 5
7 ≤ not found
102.5 at index 2
110.2 not found

std::upper_bound()

std::lower_bound()相似
官网链接:https://en.cppreference.com/w/cpp/algorithm/upper_bound
作用:返回指向范围 [first, last) 中第一个元素的迭代器,使得 value < element【或 comp(value, element)】为真(即严格大于),如果没有找到这样的元素,则返回 last。
定义:

template< class ForwardIt, class T >
ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value );
或
template< class ForwardIt, class T, class Compare >
ForwardIt upper_bound( ForwardIt first, ForwardIt last,
                       const T& value, Compare comp );

猜你喜欢

转载自blog.csdn.net/BIT_HXZ/article/details/128638494
今日推荐