std::ranges::minmax, std::ranges::minmax_result
来自cppreference.com
| 在标头 <algorithm> 定义
|
||
| 调用签名 |
||
| |
(1) | (C++20 起) |
| |
(2) | (C++20 起) |
| |
(3) | (C++20 起) |
| 辅助类型 |
||
| |
(4) | (C++20 起) |
返回给定的投影后值中的最小者与最大者。
1) 返回
a 与 b 的较小者与较大者的引用。2) 返回 initializer_list
r 中的最小与最大值。3) 返回范围
r 中的最小与最大值。此页面上描述的函数式实体是算法函数对象(非正式地称为 niebloid),即:
Parameters
| a, b | - | 要比较的值 |
| r | - | 要比较的非空的值范围 |
| comp | - | 应用到投影后元素的值 |
| proj | - | 应用到元素的投影 |
返回值
1) 若按照被投影值
b 小于 a,则为 {b, a};否则返回 {a, b}。2,3)
{s, l},其中 s 与 l 分别为 r 中按照其投影值的的最小与最大值。若数个值等价于最小与最大,则返回最左的最小值与最右的最大值。若范围为空(由 ranges::distance(r) 确定)则行为未定义。复杂度
1) 至多比较一次并投影二次。
2,3) 至多比较
3 / 2 * ranges::distance(r) 次并且应用两倍次数的投影。可能的实现
struct minmax_fn
{
template<class T, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<const T*, Proj>> Comp = ranges::less>
constexpr ranges::minmax_result<const T&>
operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const
{
if (std::invoke(comp, std::invoke(proj, b), std::invoke(proj, a)))
return {b, a};
return {a, b};
}
template<std::copyable T, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<const T*, Proj>> Comp = ranges::less>
constexpr ranges::minmax_result<T>
operator()(std::initializer_list<T> r, Comp comp = {}, Proj proj = {}) const
{
auto result = ranges::minmax_element(r, std::ref(comp), std::ref(proj));
return {*result.min, *result.max};
}
template<ranges::input_range R, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less>
requires std::indirectly_copyable_storable<ranges::iterator_t<R>,
ranges::range_value_t<R>*>
constexpr ranges::minmax_result<ranges::range_value_t<R>>
operator()(R&& r, Comp comp = {}, Proj proj = {}) const
{
auto result = ranges::minmax_element(r, std::ref(comp), std::ref(proj));
return {std::move(*result.min), std::move(*result.max)};
}
};
inline constexpr minmax_fn minmax;
|
注解
对于重载 (1),若形参之一为临时量,则返回的引用在含有对 minmax 的该调用的完全表达式结束后变为悬垂引用。
int n = 1;
auto p = std::ranges::minmax(n, n + 1);
int m = p.min; // ok
int x = p.max; // 未定义行为
// 注意,结构化绑定有相同的问题
auto [mm, xx] = std::ranges::minmax(n, n + 1);
xx; // 未定义行为
示例
运行此代码
#include <algorithm>
#include <array>
#include <iostream>
#include <random>
int main()
{
namespace ranges = std::ranges;
constexpr std::array v{3, 1, 4, 1, 5, 9, 2, 6, 5};
std::random_device rd;
std::mt19937_64 generator(rd());
std::uniform_int_distribution<> distribution(0, ranges::distance(v)); // [0..9]
// auto bounds = ranges::minmax(distribution(generator), distribution(generator));
// UB: 悬垂引用: bounds.min 和 bounds.max 类型为 `const int&`。
const int x1 = distribution(generator);
const int x2 = distribution(generator);
auto bounds = ranges::minmax(x1, x2); // OK: 获得对左值 x1 和 x2 的引用
std::cout << "v[" << bounds.min << ":" << bounds.max << "]: ";
for (int i = bounds.min; i < bounds.max; ++i)
std::cout << v[i] << ' ';
std::cout << '\n';
auto [min, max] = ranges::minmax(v);
std::cout << "最小值: " << min << ", " << "最大值: " << max << '\n';
}
可能的输出:
v[3:9]: 1 5 9 2 6 5
最小值: 1, 最大值: 9
