| Lecture 6 - Slide 20 : 40 |
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;
} Implementation and sample use of for-each on a list of integers. |
Use of std::for-each instead of our own definition of for-each. |