// functor.cpp -- using a functor #include #include #include #include template // functor class defines operator()() class TooBig { private: T cutoff; public: TooBig(const T & t) : cutoff(t) {} bool operator()(const T & v) { return v > cutoff; } }; void outint(int n) {std::cout << n << " ";} int main() { using std::list; using std::cout; using std::endl; using std::for_each; using std::remove_if; TooBig f100(100); // limit = 100 int vals[10] = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201}; list yadayada(vals, vals + 10); // range constructor list etcetera(vals, vals + 10); // C++0x can use the following instead // list yadayada = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201}; // list etcetera {50, 100, 90, 180, 60, 210, 415, 88, 188, 201}; cout << "Original lists:\n"; for_each(yadayada.begin(), yadayada.end(), outint); cout << endl; for_each(etcetera.begin(), etcetera.end(), outint); cout << endl; yadayada.remove_if(f100); // use a named function object etcetera.remove_if(TooBig(200)); // construct a function object cout <<"Trimmed lists:\n"; for_each(yadayada.begin(), yadayada.end(), outint); cout << endl; for_each(etcetera.begin(), etcetera.end(), outint); cout << endl; // std::cin.get(); return 0; }