跳到主要内容

std::lower_bound() 算法

// (1)
template< class ForwardIt, class T >
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );

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

返回一个迭代器,指向范围 [first; last) 中第一个使得 element < value(或 comp(element, value))为 false 的元素(即大于或等于 value 的元素),如果未找到此类元素,则返回 last

重要

范围 [first; last) 必须根据表达式 element < value(或 comp(element, value))进行分区,即所有使表达式为 true 的元素必须位于所有使表达式为 false 的元素之前。

完全排序的范围满足此条件。

  • (1) 使用 operator< 比较元素。
  • (2) 使用给定的比较函数 comp

参数

first
last

要检查的部分有序范围。

用于比较元素的 `value`。

comp

比较函数对象(即满足 Compare 要求的对象)。比较函数的签名应等同于以下内容:

bool cmp(const Type1 &a, const Type2 &b);
  • 签名不需要有 `const&`,但不得修改参数。
  • 必须接受 TypeType2(可能是 const)的所有类型值,无论值类别如何(因此不允许 Type1&除非对于 Type1 移动等同于复制,否则也不允许 Type1 (C++11 起)
  • `Type1` 和 `Type2` 类型必须是 `RandomIt` 类型的对象可以隐式转换为它们两者的类型。

类型要求

ForwardItLegacyForwardIterator
CompareBinaryPredicate

`Compare` 不要求满足 Compare

返回值

迭代器,指向范围 [first; last) 中第一个使得 element < value(或 comp(element, value))为 false 的元素,如果未找到此类元素,则返回 last

复杂度

执行的比较次数与 firstlast 之间的距离呈对数关系(最多 log^2(last - first) + O(1) 次比较)。

然而,对于非 LegacyRandomAccessIterators,迭代器增量次数是线性的。

值得注意的是,std::mapstd::multimapstd::setstd::multiset 迭代器不是随机访问迭代器,因此应优先使用它们的成员 lower_bound() 函数。

异常

(无)

可能的实现

lower_bound (1)
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;
}
lower_bound (2)
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;
}

示例

Main.cpp
#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
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。单击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::lower_bound() 算法

// (1)
template< class ForwardIt, class T >
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );

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

返回一个迭代器,指向范围 [first; last) 中第一个使得 element < value(或 comp(element, value))为 false 的元素(即大于或等于 value 的元素),如果未找到此类元素,则返回 last

重要

范围 [first; last) 必须根据表达式 element < value(或 comp(element, value))进行分区,即所有使表达式为 true 的元素必须位于所有使表达式为 false 的元素之前。

完全排序的范围满足此条件。

  • (1) 使用 operator< 比较元素。
  • (2) 使用给定的比较函数 comp

参数

first
last

要检查的部分有序范围。

用于比较元素的 `value`。

comp

比较函数对象(即满足 Compare 要求的对象)。比较函数的签名应等同于以下内容:

bool cmp(const Type1 &a, const Type2 &b);
  • 签名不需要有 `const&`,但不得修改参数。
  • 必须接受 TypeType2(可能是 const)的所有类型值,无论值类别如何(因此不允许 Type1&除非对于 Type1 移动等同于复制,否则也不允许 Type1 (C++11 起)
  • `Type1` 和 `Type2` 类型必须是 `RandomIt` 类型的对象可以隐式转换为它们两者的类型。

类型要求

ForwardItLegacyForwardIterator
CompareBinaryPredicate

`Compare` 不要求满足 Compare

返回值

迭代器,指向范围 [first; last) 中第一个使得 element < value(或 comp(element, value))为 false 的元素,如果未找到此类元素,则返回 last

复杂度

执行的比较次数与 firstlast 之间的距离呈对数关系(最多 log^2(last - first) + O(1) 次比较)。

然而,对于非 LegacyRandomAccessIterators,迭代器增量次数是线性的。

值得注意的是,std::mapstd::multimapstd::setstd::multiset 迭代器不是随机访问迭代器,因此应优先使用它们的成员 lower_bound() 函数。

异常

(无)

可能的实现

lower_bound (1)
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;
}
lower_bound (2)
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;
}

示例

Main.cpp
#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
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。单击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。