Back to TILs

C++ Ranges — Part 3 — The simplest C++ algorithm: std::for_each

Date: 2023-03-02Last modified: 2023-03-06

Table of contents

Copy of post Daily bit(e) of C++ | std::for_each written by Šimon Tóth.

The std::for_each algorithm invokes the provided functor on each range element in order, ignoring the result.

While the range-based for-loop has mostly replaced the use cases for std::for_each, it still comes in handy as a trivial parallelization tool and in its C++20 range version with projections.

Possible output

6 6 6 6 6 
0 2 0 4 0