? / professional-program-22-ModifyingAlgorithms-Transform.c
include <algorithm> include <functional> include <iostream> include <vector> using namespace std; // Function template to populate a container of ints. // The container must support push_back(). template<typename Container> void populateContainer(Container& cont) { int num; while (true) { cout << "Enter a number (0 to quit): "; cin >> num; if (num == 0) { break; } cont.push_back(num); } } void print(int elem) { cout << elem << " "; } int main(int argc, char** argv) { vector<int> myVector; populateContainer(myVector); cout << "The vector contents are:\n"; for_each(myVector.begin(), myVector.end(), &print); cout << endl; transform(myVector.begin(), myVector.end(), myVector.begin(), bind2nd(plus<int>(), 100)); cout << "The vector contents are:\n"; for_each(myVector.begin(), myVector.end(), &print); cout << endl; return (0); }
(C) Æliens 20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.