跳到主要内容

std::ranges::minmax_element() 算法

// (1)
constexpr minmax_element_result<I>
minmax_element( I first, S last, Comp comp = {}, Proj proj = {} );

// (2)
constexpr minmax_element_result<ranges::borrowed_iterator_t<R>>
minmax_element( R&& r, Comp comp = {}, Proj proj = {} );

参数类型是泛型的,并具有以下约束

  • I - std::forward_iterator
  • S - std::sentinel_for<I>
  • R - std::ranges::forward_range
  • 比较:
    • (1) - std::indirect_strict_weak_order<std::projected<I, Proj>>
    • (2) - std::indirect_strict_weak_order<std::projected<ranges::iterator_t<R>, Proj>>
  • Proj - (无)

所有重载的 ProjComp 模板参数具有以下默认类型:std::identity, ranges::less

辅助类型定义如下:

template< class I >
using minmax_element_result = ranges::min_max_result<I>;
  • (1) 在范围 [first; last) 中查找最小和最大的元素。

  • (2)(1) 相同,但使用 r 作为源范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

参数

first
last

要查找最小和最大值的范围。

r

要查找最小和最大值的范围。

comp

应用于投影元素的比较。

proj

应用于元素的投影。

返回值

一个对象,第一个元素是指向最小元素的迭代器,第二个元素是指向最大元素的迭代器。

如果范围为空,则返回 { first, first }

如果有多个元素与最小元素等价,则返回指向第一个此类元素的迭代器。
如果有多个元素与最大元素等价,则返回指向最后一个此类元素的迭代器。

复杂度

给定 Nranges::distance(first, last)

最多应用 max(floor((3 / 2) * (N − 1)), 0) 次比较和两倍的投影。

异常

(无)

可能的实现

minmax_element(1) 和 minmax_element(2)
struct minmax_element_fn
{
template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
std::indirect_strict_weak_order<std::projected<I, Proj>> Comp = ranges::less>
constexpr ranges::minmax_element_result<I>
operator()(I first, S last, Comp comp = {}, Proj proj = {}) const
{
auto min = first, max = first;

if (first == last || ++first == last) {
return {min, max};
}

if (std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *min))) {
min = first;
} else {
max = first;
}

while (++first != last) {
auto i = first;
if (++first == last) {
if (std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *min))) {
min = i;
}
else if (!(std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *max)))) {
max = i;
}
break;
} else {
if (std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *i))) {
if (std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *min))) {
min = first;
}
if (!(std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *max)))) {
max = i;
}
} else {
if (std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *min))) {
min = i;
}
if (!(std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *max)))) {
max = first;
}
}
}
}
return {min, max};
}

template<ranges::forward_range R, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less>
constexpr ranges::minmax_element_result<ranges::borrowed_iterator_t<R>>
operator()(R&& r, Comp comp = {}, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::ref(comp), std::ref(proj));
}
};

inline constexpr minmax_element_fn minmax_element;

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <iterator>
namespace rng = std::ranges;

int main()
{
const auto v = {3, 9, 1, 4, 1, 2, 5, 9};
const auto [min, max] = rng::minmax_element(v);
std::cout
<< "min = " << *min << ", at [" << rng::distance(v.begin(), min) << "]\n"
<< "max = " << *max << ", at [" << rng::distance(v.begin(), max) << "]\n";
}
输出
min = 1, at [2]
max = 9, at [7]
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”以查看此文档的所有更改。
悬停查看原始许可证。

std::ranges::minmax_element() 算法

// (1)
constexpr minmax_element_result<I>
minmax_element( I first, S last, Comp comp = {}, Proj proj = {} );

// (2)
constexpr minmax_element_result<ranges::borrowed_iterator_t<R>>
minmax_element( R&& r, Comp comp = {}, Proj proj = {} );

参数类型是泛型的,并具有以下约束

  • I - std::forward_iterator
  • S - std::sentinel_for<I>
  • R - std::ranges::forward_range
  • 比较:
    • (1) - std::indirect_strict_weak_order<std::projected<I, Proj>>
    • (2) - std::indirect_strict_weak_order<std::projected<ranges::iterator_t<R>, Proj>>
  • Proj - (无)

所有重载的 ProjComp 模板参数具有以下默认类型:std::identity, ranges::less

辅助类型定义如下:

template< class I >
using minmax_element_result = ranges::min_max_result<I>;
  • (1) 在范围 [first; last) 中查找最小和最大的元素。

  • (2)(1) 相同,但使用 r 作为源范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

参数

first
last

要查找最小和最大值的范围。

r

要查找最小和最大值的范围。

comp

应用于投影元素的比较。

proj

应用于元素的投影。

返回值

一个对象,第一个元素是指向最小元素的迭代器,第二个元素是指向最大元素的迭代器。

如果范围为空,则返回 { first, first }

如果有多个元素与最小元素等价,则返回指向第一个此类元素的迭代器。
如果有多个元素与最大元素等价,则返回指向最后一个此类元素的迭代器。

复杂度

给定 Nranges::distance(first, last)

最多应用 max(floor((3 / 2) * (N − 1)), 0) 次比较和两倍的投影。

异常

(无)

可能的实现

minmax_element(1) 和 minmax_element(2)
struct minmax_element_fn
{
template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
std::indirect_strict_weak_order<std::projected<I, Proj>> Comp = ranges::less>
constexpr ranges::minmax_element_result<I>
operator()(I first, S last, Comp comp = {}, Proj proj = {}) const
{
auto min = first, max = first;

if (first == last || ++first == last) {
return {min, max};
}

if (std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *min))) {
min = first;
} else {
max = first;
}

while (++first != last) {
auto i = first;
if (++first == last) {
if (std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *min))) {
min = i;
}
else if (!(std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *max)))) {
max = i;
}
break;
} else {
if (std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *i))) {
if (std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *min))) {
min = first;
}
if (!(std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *max)))) {
max = i;
}
} else {
if (std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *min))) {
min = i;
}
if (!(std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *max)))) {
max = first;
}
}
}
}
return {min, max};
}

template<ranges::forward_range R, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less>
constexpr ranges::minmax_element_result<ranges::borrowed_iterator_t<R>>
operator()(R&& r, Comp comp = {}, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::ref(comp), std::ref(proj));
}
};

inline constexpr minmax_element_fn minmax_element;

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <iterator>
namespace rng = std::ranges;

int main()
{
const auto v = {3, 9, 1, 4, 1, 2, 5, 9};
const auto [min, max] = rng::minmax_element(v);
std::cout
<< "min = " << *min << ", at [" << rng::distance(v.begin(), min) << "]\n"
<< "max = " << *max << ", at [" << rng::distance(v.begin(), max) << "]\n";
}
输出
min = 1, at [2]
max = 9, at [7]
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”以查看此文档的所有更改。
悬停查看原始许可证。