// ========================================================================== // $Id: functor_lambda.cpp,v 1.1 2014/11/29 00:31:45 jlang Exp $ // CSI2372 example Code for tutorial // ========================================================================== // (C)opyright: // // Jochen Lang // EECS, University of Ottawa // 800 King Edward Ave. // Ottawa, On., K1N 6N5 // Canada. // http://www.eecs.uottawa.ca // // Creator: jlang (Jochen Lang) // Email: jlang@eecs.uottawa.ca // ========================================================================== // $Log: functor_lambda.cpp,v $ // Revision 1.1 2014/11/29 00:31:45 jlang // Check-in of in-class version // // ========================================================================== #include #include #include #include using std::cout; using std::endl; #define USE_LAMBDA struct iFunc { int& d_count; iFunc(int& _count) : d_count(_count){}; bool operator()(int i0, int i1){ ++d_count; return i0 iVec(100,0); // srand to seed? // range loop for (int& element:iVec ){ element = std::rand()%10+1; } int count = 0; #ifdef USE_LAMBDA std::sort(iVec.begin(),iVec.end(), [&](int i0, int i1){++count; return i0 < i1;}); #else iFunc ifu(count); std::sort(iVec.begin(),iVec.end(),ifu); #endif // for_each to print std::for_each(iVec.begin(),iVec.end(), [](int element) { cout << element << " ";}); cout << "Count: " << count << endl; return 0; }