//: C07:Apply.cpp // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. // Using simple iteration. #include #include #include using namespace std; template void apply(Cont& c, PtrMemFun f) { typename Cont::iterator it = c.begin(); while(it != c.end()) { ((*it).*f)(); // Alternate form ++it; } } class Z { int i; public: Z(int ii) : i(ii) {} void g() { ++i; } friend ostream& operator<<(ostream& os, const Z& z) { return os << z.i; } }; int main() { ostream_iterator out(cout, " "); vector vz; for(int i = 0; i < 10; i++) vz.push_back(Z(i)); copy(vz.begin(), vz.end(), out); cout << endl; apply(vz, &Z::g); copy(vz.begin(), vz.end(), out); } ///:~