cpp useful lambdas code example
Example 1: cpp lambda function
#include <iostream>
using namespace std;
bool isGreater = [](int a, int b){ return a > b; }
int main() {
cout << isGreater(5, 2) << endl; // Output: 1
return 0;
}
Example 2: cpp lambda
struct X {
int x, y;
int operator()(int);
void f()
{
// the context of the following lambda is the member function X::f
[=]()->int
{
return operator()(this->x + y); // X::operator()(this->x + (*this).y)
// this has type X*
};
}
};