#include #include #include #include // for bind() using namespace std; using std::placeholders::_1; template void dumpVec(T c) { cout << "{"; for ( auto el : c ) { cout << el << ", "; } cout << "}" << endl; } void printIf(int limit, int val) { if ( val > limit ) cout << val << ' '; } //------------------------------------------- // This version uses a function class, and a // function object. //------------------------------------------- class FuncObj { public: FuncObj(int lim) { limit = lim; } // The overloaded parentheses operator allows us to // treat an object of this class as a function, at least in // terms of call syntax void operator()(int val) { printIf(limit, val); } private: int limit; }; int main(int argc, char *argv[]) { vector V = { -1, 40, 101, 12, 0, 0, 102, 4, 8, 103}; cout << "V = "; dumpVec(V); int limit; cout << endl << "Enter limit value: "; cin >> limit; cout << "Filtered[>" << limit << "]: "; // bind() causes the compiler to generate an adapater method // at compile time. // In this case the adapter's code would look something like this: // void some_name(int a) { // printf(, a); // } // A pointer to 'some_name' method would actually be provided // as the argument to for_each. FuncObj fn(limit); for_each(V.begin(), V.end(), fn); cout << endl; return 0; }