std::for_each – cppreference.com
Aus cppreference.com
<metanoindex/>
<tbody> </tbody>
| definiert in Header <algorithm> |
||
|
|
||
Wendet die angegebene Funktion Objekt f dem Ergebnis der Dereferenzierung jeden Iterator im Bereich [first, last), in Ordnung. Wenn InputIt eine veränderliche Iterator ist, kann f die Elemente des Bereichs durch die dereferenziert Iterator ändern. Wenn f ein Ergebnis zurückgibt, wird das Ergebnis ignoriert .
Original:
Applies the given function object f to the result of dereferencing every iterator in the range [first, last), in order. If InputIt is a mutable iterator, f may modify the elements of the range through the dereferenced iterator. If f returns a result, the result is ignored.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Parameter
| first, last | - | der Bereich um die Funktion zu verarbeiten Original: the range to apply the function to The text has been machine-translated via Google Translate. |
| f | - | die einstellige Funktion Objekt angewendet werden Original: the unary function object to be applied The text has been machine-translated via Google Translate. |
| Type requirements | ||
-InputIt must meet the requirements of InputIterator.
| ||
-UnaryFunction must meet the requirements of MoveConstructible. Does not have to be CopyConstructible
| ||
Rückgabewert
f. (bis C + +11)
std::move(f). (seit C++11)
Komplexität
Genau last - first Anwendungen f
Original:
Exactly last - first applications of f
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Mögliche Implementierung
template<class InputIt, class UnaryFunction> UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f) { for (; first != last; ++first) { f(*first); } return f; }
Beispiel
Im folgenden Beispiel wird ein Lambda-Funktion um alle Elemente eines Vektors inkrementieren und berechnet dann eine Summe von ihnen:
Original:
The following example uses a Lambda-Funktion to increment all of the elements of a vector and then computes a sum of them:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#include <vector> #include <algorithm> #include <iostream> struct Sum { Sum() { sum = 0; } void operator()(int n) { sum += n; } int sum; }; int main() { std::vector<int> nums{3, 4, 2, 9, 15, 267}; std::cout << "before: "; for (auto n : nums) { std::cout << n << " "; } std::cout << '\n'; std::for_each(nums.begin(), nums.end(), [](int &n){ n++; }); Sum s = std::for_each(nums.begin(), nums.end(), Sum()); std::cout << "after: "; for (auto n : nums) { std::cout << n << " "; } std::cout << '\n'; std::cout << "sum: " << s.sum << '\n'; }
Output:
before: 3 4 2 9 15 267 after: 4 5 3 10 16 268 sum: 306
Siehe auch
gilt eine Funktion einer Reihe von Elementen Original: applies a function to a range of elements The text has been machine-translated via Google Translate. (Funktions-Template) [edit] | |
| range-for-Schleife | führt eine Schleife über Spektrum (seit C++11) Original: executes loop over range (seit C++11) The text has been machine-translated via Google Translate. |