for_each()

for_each() takes a third parameter that represents an operation that is applied to each element in turn. The operation cannot modify the elements (we can use transform() for that). Although the operation may return a value, that value is ignored.

#include <algorithm> 
template <typename Type> 
    void print_elements( Type elem ) { cout << elem << " "; } 

for_each( ivec.begin(), ivec.end(), print_elements ); 

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.