Lecture 6 - Slide 20 : 40 |

The for-each algorithm

In C++ ** for-each** is an algorithm, not a control structure like

C++11 has introduced the 'range-** for** loop' similar to

// A possible definition of the for_each function template.template <typename InputIt, typename Function> Function for_each(InputIt first, InputIt last, Function f){ while (first != last) f(*first++);// Notice the use of operator() on freturn f; }

- A Stroustrup advice:
*"In general, before using for_each(), consider if there is a more specialized algorithm that would do more for you"*- Such as
`find()`or`accumulate()`

- Such as

- Observation about the return-value of
`for_each`- The function
is returned.`f`- Later we will see an example where this is convenient

- The function