#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; } //------------------------------------------------ // We end up being able to use this two argument // version of printIf(), which solves the problem // of having a variable filter limit. //------------------------------------------------ void printIf(int limit, int val) { if ( val > limit ) cout << val << ' '; } //------------------------------------------------ // This version uses std::bind() to generate // a customized adapter method ("shim") between // for_each and printIf(). //------------------------------------------------ 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() is templated, and so its use causes the compiler to // generate a customized adapter 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 the 'some_name' method would actually be provided // as the argument to for_each. for_each(V.begin(), V.end(), bind(printIf, limit, _1)); cout << endl; return 0; }