C++ Standard Library Algorithms std::for_each

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

template<class InputIterator, class Function>
    Function for_each(InputIterator first, InputIterator last, Function f);

Effects:

Applies f to the result of dereferencing every iterator in the range [first, last) starting from first and proceeding to last - 1.

Parameters:

first, last - the range to apply f to.

f - callable object which is applied to the result of dereferencing every iterator in the range [first, last).

Return value:

f (until C++11) and std::move(f) (since C++11).

Complexity:

Applies f exactly last - first times.

Example:

c++11
std::vector<int> v { 1, 2, 4, 8, 16 };
std::for_each(v.begin(), v.end(), [](int elem) { std::cout << elem << " "; });

Applies the given function for every element of the vector v printing this element to stdout.



Got any C++ Question?