Namespaces
Variants
Views
Actions

std::ranges::minmax

From cppreference.com
< cpp‎ | algorithm‎ | ranges
Revision as of 10:54, 1 July 2020 by Cjdb (Talk | contribs)

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
       
       
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
       
       
minmax
Permutation operations
Fold operations
Numeric operations
(C++23)            
Operations on uninitialized storage
Return types
 
Defined in header <algorithm>
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&>

minmax( const T& a, const T& b, Comp comp = {}, Proj proj = {} );
(1) (since C++20)
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&>

minmax( std::initializer_list<T> r, Comp comp = {}, Proj proj = {} );
(2) (since C++20)
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>>

minmax( R&& r, Comp comp = {}, Proj proj = {} );
(3) (since C++20)
template< class T >
using ranges::minmax_result = min_max_result<T>;
(4) (since C++20)

Returns the smaller of the given projected values.

1) Returns references to the smaller and the greater of a and b.
2) Returns the smallest and the greatest of the values in the initializer list r.
3) Returns the smallest and the greatest of the values in the range r.

The function-like entities described on this page are niebloids, that is:

In practice, they may be implemented as function objects, or with special compiler extensions.

Contents

Parameters

a, b - the values to compare
r - a non-empty range of values to compare
comp - comparison to apply to the projected elements
proj - projection to apply to the elements

Return value

1) {b, a} if, according to their respective projections, b is smaller than a; otherwise it returns {a, b}.
2-3) {s, l}, where s and l are respectively the smallest and largest values in r, according to their respective projections. If several values are equivalent to the smallest and largest, returns the leftmost smallest value, and the rightmost largest value.

Complexity

1) Exactly one comparison and two applications of the projection
2-3) At most 3 / 2 * ranges::distance(r) comparisons and twice as many applications of the projection

Possible implementation

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<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()(
      std::initializer_list<T> 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)};
  }
 
  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;

Notes

For overloads (1,2), if one of the parameters is an rvalue, the reference returned becomes a dangling reference at the end of the full expression that contains the call to minmax:

int n = 1;
auto p = ranges::minmax(n, n+1);
int m = p.first; // ok
int x = p.second; // undefined behavior


Example

#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
 
int main()
{
    std::vector<int> v{3, 1, 4, 9, 1, 5, 9, 2, 6};
    std::mt19937_64 generator;
    std::uniform_int_distribution<> distribution(0, ranges::distance(v));
 
    namespace ranges = std::ranges;
    auto bounds = ranges::minmax(distribution(generator), distribution(generator));
 
    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 << "smallest: " << min << '\n'
              << "largest: " << max << '\n';
}

Possible output:

v[2:7]: 4 9 1 5 9
smallest: 1
largest: 9

See also

returns the smaller of the given values
(niebloid)[edit]
returns the greater of the given values
(niebloid)[edit]
ranges::minmax
(C++20)
returns the smaller and larger of two elements
(niebloid)[edit]
returns the smallest and the largest elements in a range
(niebloid)[edit]
clamps a value between a pair of boundary values
(niebloid)[edit]
(C++11)
returns the smaller and larger of two elements
(function template) [edit]