C++ Standard Library Algorithms std::for_each


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.