#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 labmdas // (anonymous functions). //------------------------------------------- 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 << "]: "; //------------------------------------------------------------------------------- // Lambda's are equivalent to the compiler automatically creating // a customized function class/object. // The lambda syntax is weird: [capture list] (parameter list ) -> return type { code } //------------------------------------------------------------------------------- for_each(V.begin(), V.end(), [limit](int val){ printIf(limit, val); } ); cout << endl; return 0; }