Lecture 6 - Slide 20 : 40
The for-each algorithm

In C++ for-each is an algorithm, not a control structure like foreach in C#

C++11 has introduced the 'range-for loop' similar to foreach in C#

// 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 f 
  return f;
}
for-each-reproduction-1.cpp
Implementation and sample use of for-each on an C-style array.
for-each-reproduction-2.cpp
Implementation and sample use of for-each on a list of integers.
for-each-real-use.cpp
Use of std::for-each instead of our own definition of for-each.