跳到主要内容

std::upper_bound() 算法

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

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

返回一个迭代器,指向范围 [first; last) 中第一个满足 value < element (或 comp(value, element)) 为 true 的元素(即严格大于 value 的元素),如果没有找到这样的元素,则返回 last

范围 [first; last) 必须根据表达式 !(value < element)!comp(value, element) 进行分区,即,所有表达式为 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) 中第一个满足 value < element (或 comp(value, element)) 为 true 的元素,如果没有找到这样的元素,则返回 last

复杂度

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

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

异常

(无)

可能的实现

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

示例

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 < 7; ++i)
{
// Search first element that is greater than i
auto upper = std::upper_bound(data.begin(), data.end(), i);

std::cout << i << " < ";
upper != data.end()
? std::cout << *upper << " at index " << std::distance(data.begin(), upper)
: 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::upper_bound(prices.begin(), prices.end(), to_find,
[](double value, const PriceInfo& info)
{
return value < info.price;
});

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 < 2 at index 1
2 < 4 at index 2
3 < 4 at index 2
4 < 5 at index 3
5 < 6 at index 5
6 < not found
107.3 at index 4
110.2 not found
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::upper_bound() 算法

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

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

返回一个迭代器,指向范围 [first; last) 中第一个满足 value < element (或 comp(value, element)) 为 true 的元素(即严格大于 value 的元素),如果没有找到这样的元素,则返回 last

范围 [first; last) 必须根据表达式 !(value < element)!comp(value, element) 进行分区,即,所有表达式为 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) 中第一个满足 value < element (或 comp(value, element)) 为 true 的元素,如果没有找到这样的元素,则返回 last

复杂度

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

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

异常

(无)

可能的实现

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

示例

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 < 7; ++i)
{
// Search first element that is greater than i
auto upper = std::upper_bound(data.begin(), data.end(), i);

std::cout << i << " < ";
upper != data.end()
? std::cout << *upper << " at index " << std::distance(data.begin(), upper)
: 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::upper_bound(prices.begin(), prices.end(), to_find,
[](double value, const PriceInfo& info)
{
return value < info.price;
});

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 < 2 at index 1
2 < 4 at index 2
3 < 4 at index 2
4 < 5 at index 3
5 < 6 at index 5
6 < not found
107.3 at index 4
110.2 not found
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。