跳到主要内容

std::ranges::min_element() 算法

// (1)
constexpr I min_element( I first, S last, Comp comp = {}, Proj proj = {} );

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

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

  • I - std::forward_iterator
  • S - std::sentinel_for<I>
  • R - std::ranges::forward_range
  • Comp:
    • (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

此外,每个重载都有以下约束

  • (1) - std::sortable<I, Comp, Proj>
  • (2) - std::sortable<ranges::iterator_t<R>, Comp, Proj>
  • (1) 查找范围 [first; last) 中的最小元素。

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

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

参数

first
last

要查找最小值的范围。

r

要查找最小值的范围。

comp

应用于投影元素的比较。

proj

应用于元素的投影。

返回值

指向范围 [first; last) 中最小元素的迭代器。

如果范围中有多个元素与最小元素等效,则返回指向第一个此类元素的迭代器。

如果范围为空,则返回 first

复杂度

给定 Nstd::distance(first, last)

比较次数恰好为 min(N - 1, 0)

异常

(无)

可能的实现

min_element(1) 和 min_element(2)
struct min_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 I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const
{
if (first == last)
return last;

auto smallest = first;
++first;
for (; first != last; ++first)
if (!std::invoke(comp, std::invoke(proj, *smallest), std::invoke(proj, *first)))
smallest = first;
return smallest;
}

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::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 min_element_fn min_element;

示例

Main.cpp
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

int main()
{
std::vector<int> v {3, 1, -14, 1, 5, 9};

namespace ranges = std::ranges;
auto result = ranges::min_element(v.begin(), v.end());
std::cout << "min element at [" << ranges::distance(v.begin(), result)
<< "]\n";

auto abs_compare = [](int a, int b) { return (std::abs(a) < std::abs(b)); };
result = ranges::min_element(v, abs_compare);
std::cout << "|min| element at [" << ranges::distance(v.begin(), result)
<< "]\n";
}
输出
min element at [2]
|min| element at [1]
本文源自此 CppReference 页面。它可能因改进或编辑者偏好而有所改动。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。

std::ranges::min_element() 算法

// (1)
constexpr I min_element( I first, S last, Comp comp = {}, Proj proj = {} );

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

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

  • I - std::forward_iterator
  • S - std::sentinel_for<I>
  • R - std::ranges::forward_range
  • Comp:
    • (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

此外,每个重载都有以下约束

  • (1) - std::sortable<I, Comp, Proj>
  • (2) - std::sortable<ranges::iterator_t<R>, Comp, Proj>
  • (1) 查找范围 [first; last) 中的最小元素。

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

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

参数

first
last

要查找最小值的范围。

r

要查找最小值的范围。

comp

应用于投影元素的比较。

proj

应用于元素的投影。

返回值

指向范围 [first; last) 中最小元素的迭代器。

如果范围中有多个元素与最小元素等效,则返回指向第一个此类元素的迭代器。

如果范围为空,则返回 first

复杂度

给定 Nstd::distance(first, last)

比较次数恰好为 min(N - 1, 0)

异常

(无)

可能的实现

min_element(1) 和 min_element(2)
struct min_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 I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const
{
if (first == last)
return last;

auto smallest = first;
++first;
for (; first != last; ++first)
if (!std::invoke(comp, std::invoke(proj, *smallest), std::invoke(proj, *first)))
smallest = first;
return smallest;
}

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::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 min_element_fn min_element;

示例

Main.cpp
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

int main()
{
std::vector<int> v {3, 1, -14, 1, 5, 9};

namespace ranges = std::ranges;
auto result = ranges::min_element(v.begin(), v.end());
std::cout << "min element at [" << ranges::distance(v.begin(), result)
<< "]\n";

auto abs_compare = [](int a, int b) { return (std::abs(a) < std::abs(b)); };
result = ranges::min_element(v, abs_compare);
std::cout << "|min| element at [" << ranges::distance(v.begin(), result)
<< "]\n";
}
输出
min element at [2]
|min| element at [1]
本文源自此 CppReference 页面。它可能因改进或编辑者偏好而有所改动。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。