Namespaces
Variants

std::ranges:: max_element

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
Definiert im Header <algorithm>
Aufrufsignatur
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

max_element ( I first, S last, Comp comp = { } , Proj proj = { } ) ;
(1) (seit C++20)
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 >

max_element ( R && r, Comp comp = { } , Proj proj = { } ) ;
(2) (seit C++20)
1) Findet das größte Element im Bereich [ first , last ) .
2) Gleich wie (1) , verwendet jedoch r als Quellbereich, als ob ranges:: begin ( r ) als first und ranges:: end ( r ) als last verwendet würde.

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

Inhaltsverzeichnis

Parameter

first, last - das Iterator-Sentinel-Paar, das den Bereich der zu untersuchenden Elemente definiert
r - der zu untersuchende range
comp - Vergleich, der auf die projizierten Elemente angewendet wird
proj - Projektion, die auf die Elemente angewendet wird

Rückgabewert

Iterator zum größten Element im Bereich [ first , last ) . Wenn mehrere Elemente im Bereich dem größten Element entsprechen, wird der Iterator zum ersten solchen Element zurückgegeben. Gibt den Iterator zurück, der gleich last ist, wenn der Bereich leer ist (d.h. wenn first == last ).

Komplexität

Genau max(N - 1, 0) Vergleiche, wobei N = ranges:: distance ( first, last ) .

Mögliche Implementierung

struct max_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 largest = first;
        while (++first != last)
            if (std::invoke(comp, std::invoke(proj, *largest), std::invoke(proj, *first)))
                largest = first;
        return largest;
    }
    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 max_element_fn max_element;

Beispiel

#include <algorithm>
#include <cmath>
#include <iostream>
int main()
{
    namespace ranges = std::ranges;
    const auto v = {3, 1, -14, 1, 5, 9, -14, 9};
    auto result = ranges::max_element(v.begin(), v.end());
    std::cout << "Max element at pos " << ranges::distance(v.begin(), result) << '\n';
    auto abs_compare = [](int a, int b) { return std::abs(a) < std::abs(b); };
    result = ranges::max_element(v, abs_compare);
    std::cout << "Absolute max element at pos "
              << ranges::distance(v.begin(), result) << '\n';
}

Ausgabe:

Max element at pos 5
Absolute max element at pos 2

Siehe auch

gibt das kleinste Element in einem Bereich zurück
(Algorithmus-Funktionsobjekt)
gibt die kleinsten und größten Elemente in einem Bereich zurück
(Algorithmus-Funktionsobjekt)
gibt den größeren der gegebenen Werte zurück
(Algorithmus-Funktionsobjekt)
gibt das größte Element in einem Bereich zurück
(Funktions-Template)