Namespaces
Variants

std::ranges:: for_each_n, std::ranges:: for_each_n_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
Definiert im Header <algorithm>
Aufrufsignatur
template < std:: input_iterator I, class Proj = std:: identity ,

std:: indirectly_unary_invocable < std :: projected < I, Proj >> Fun >
constexpr for_each_n_result < I, Fun >

for_each_n ( I first, std:: iter_difference_t < I > n, Fun f, Proj proj = { } ) ;
(1) (seit C++20)
Hilfstypen
template < class I, class F >
using for_each_n_result = ranges:: in_fun_result < I, F > ;
(2) (seit C++20)
1) Wendet das gegebene Funktionsobjekt f auf das projizierte Ergebnis von proj des Dereferenzierens jedes Iterators im Bereich [ first , first + n ) an, in Reihenfolge.

Wenn der Iteratortyp veränderbar ist, f kann die Elemente des Bereichs durch den dereferenzierten Iterator modifizieren. Wenn f ein Ergebnis zurückgibt, wird das Ergebnis ignoriert. Wenn n kleiner als null ist, ist das Verhalten undefiniert.

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

Inhaltsverzeichnis

Parameter

first - Iterator, der den Beginn des Bereichs bezeichnet, auf den die Funktion angewendet werden soll
n - Die Anzahl der Elemente, auf die die Funktion angewendet werden soll
f - Die Funktion, die auf den projizierten Bereich angewendet werden soll [ first , first + n )
proj - Projektion, die auf die Elemente angewendet werden soll

Rückgabewert

Ein Objekt { first + n, std :: move ( f ) } , wobei first + n ausgewertet werden kann als std :: ranges:: next ( std :: move ( first ) , n ) abhängig von der Iteratorkategorie.

Komplexität

Genau n Anwendungen von f und proj .

Mögliche Implementierung

struct for_each_n_fn
{
    template<std::input_iterator I, class Proj = std::identity,
             std::indirectly_unary_invocable<std::projected<I, Proj>> Fun>
    constexpr for_each_n_result<I, Fun>
        operator()(I first, std::iter_difference_t<I> n, Fun fun, Proj proj = Proj{}) const
    {
        for (; n-- > 0; ++first)
            std::invoke(fun, std::invoke(proj, *first));
        return {std::move(first), std::move(fun)};
    }
};
inline constexpr for_each_n_fn for_each_n {};

Beispiel

#include <algorithm>
#include <array>
#include <iostream>
#include <ranges>
#include <string_view>
struct P
{
    int first;
    char second;
    friend std::ostream& operator<<(std::ostream& os, const P& p)
    {
        return os << '{' << p.first << ",'" << p.second << "'}";
    }
};
auto print = [](std::string_view name, auto const& v)
{
    std::cout << name << ": ";
    for (auto n = v.size(); const auto& e : v)
        std::cout << e << (--n ? ", " : "\n");
};
int main()
{
    std::array a {1, 2, 3, 4, 5};
    print("a", a);
    // Erste drei Zahlen negieren:
    std::ranges::for_each_n(a.begin(), 3, [](auto& n) { n *= -1; });
    print("a", a);
    std::array s { P{1,'a'}, P{2, 'b'}, P{3, 'c'}, P{4, 'd'} };
    print("s", s);
    // Datenelemente 'P::first' mittels Projektion negieren:
    std::ranges::for_each_n(s.begin(), 2, [](auto& x) { x *= -1; }, &P::first);
    print("s", s);
    // Datenelemente 'P::second' mittels Projektion großschreiben:
    std::ranges::for_each_n(s.begin(), 3, [](auto& c) { c -= 'a'-'A'; }, &P::second);
    print("s", s);
}

Ausgabe:

a: 1, 2, 3, 4, 5
a: -1, -2, -3, 4, 5
s: {1,'a'}, {2,'b'}, {3,'c'}, {4,'d'}
s: {-1,'a'}, {-2,'b'}, {3,'c'}, {4,'d'}
s: {-1,'A'}, {-2,'B'}, {3,'C'}, {4,'d'}

Siehe auch

Bereichs- for Schleife (C++11) führt Schleife über Bereich aus
wendet ein unäres Funktionsobjekt auf Elemente aus einem Bereich an
(Algorithmus-Funktionsobjekt)
(C++17)
wendet ein Funktionsobjekt auf die ersten N Elemente einer Sequenz an
(Funktionsschablone)
wendet ein unäres Funktionsobjekt auf Elemente aus einem Bereich an
(Funktionsschablone)