algorithms/for-each-implementation/for-each-real-use.cpp - Use of std::for-each instead of our own definition of for-each. | Lecture 6 - slide 20 : 40 Program 3 |
// Use the Standard Libaray for-each instead of your own version of it. #include <iostream> #include <string> #include <list> #include <algorithm> void print_element(int i){ std::cout << i << std::endl; } int main(){ std::list<int> lst; lst.push_back(7); lst.push_back(9); lst.push_back(8); lst.push_back(1); lst.push_back(-1); // The two first parameters are iterators: std::for_each(lst.begin(), lst.end(), print_element); // 7 9 8 1 -1 }