Namespaces
Variants

std::ranges:: minmax, std::ranges:: minmax_result

From cppreference.net
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
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
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
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
(Anmerkung: Der bereitgestellte HTML-Code enthält keinen übersetzbaren Text, da alle Tags leer sind. Die Struktur bleibt unverändert, wie angefordert.)
Definiert in Header <algorithm>
Aufrufsignatur
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) (seit C++20)
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 >

minmax ( std:: initializer_list < T > r, Comp comp = { } , Proj proj = { } ) ;
(2) (seit 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 >
erfordert 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) (seit C++20)
Hilfstypen
template < class T >
using minmax_result = ranges:: min_max_result < T > ;
(4) (seit C++20)

Gibt die kleinsten und größten der gegebenen projizierten Werte zurück.

1) Gibt Referenzen auf den kleineren und den größeren Wert von a und b zurück.
2) Gibt den kleinsten und den größten Wert in der Initialisierungsliste r zurück.
3) Gibt den kleinsten und den größten Wert im Bereich r zurück.

Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind Algorithm Function Objects (informell bekannt als Niebloids ), das heißt:

Inhaltsverzeichnis

Parameter

a, b - die zu vergleichenden Werte
r - ein nicht-leerer Wertebereich zum Vergleichen
comp - Vergleichsfunktion für die projizierten Elemente
proj - Projektion für die Elemente

Rückgabewert

1) { b, a } wenn gemäß ihrem jeweiligen projizierten Wert b kleiner als a ist; andernfalls gibt es { a, b } zurück.
2,3) { s, l } , wobei s und l jeweils den kleinsten und größten Wert in r gemäß ihres projizierten Wertes darstellen. Falls mehrere Werte dem kleinsten und größten Wert entsprechen, wird der am weitesten links stehende kleinste Wert und der am weitesten rechts stehende größte Wert zurückgegeben. Falls der Bereich leer ist (bestimmt durch ranges:: distance ( r ) ), ist das Verhalten undefiniert.

Komplexität

1) Genau ein Vergleich und zwei Anwendungen der Projektion.
2,3) Höchstens 3 / 2 * ranges:: distance ( r ) Vergleiche und doppelt so viele Anwendungen der Projektion.

Mögliche Implementierung

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;

Hinweise

Für Überladung (1) , wenn einer der Parameter ein temporäres Objekt ist, wird die zurückgegebene Referenz am Ende des vollständigen Ausdrucks, der den Aufruf von minmax enthält, zu einer hängenden Referenz:

int n = 1;
auto p = std::ranges::minmax(n, n + 1);
int m = p.min; // ok
int x = p.max; // undefiniertes Verhalten
// Beachten Sie, dass strukturierte Bindungen dasselbe Problem haben
auto [mm, xx] = std::ranges::minmax(n, n + 1);
xx; // undefiniertes Verhalten

Beispiel

#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: dangling references: bounds.min and bounds.max have the type `const int&`.
    const int x1 = distribution(generator);
    const int x2 = distribution(generator);
    auto bounds = ranges::minmax(x1, x2); // OK: got references to lvalues x1 and 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 << "smallest: " << min << ", " << "largest: " << max << '\n';
}

Mögliche Ausgabe:

v[3:9]: 1 5 9 2 6 5 
smallest: 1, largest: 9

Siehe auch

gibt den kleineren der gegebenen Werte zurück
(Algorithmus-Funktionsobjekt)
gibt den größeren der gegebenen Werte zurück
(Algorithmus-Funktionsobjekt)
gibt die kleinsten und größten Elemente in einem Bereich zurück
(Algorithmus-Funktionsobjekt)
begrenzt einen Wert zwischen einem Paar von Grenzwerten
(Algorithmus-Funktionsobjekt)
(C++11)
gibt die kleineren und größeren von zwei Elementen zurück
(Funktions-Template)