c++ lambda functions code example
Example 1: lambda c++
#include <iostream>
#include <string>
auto makeWalrus(const std::string& name)
{
return [&]() {
std::cout << "I am a walrus, my name is " << name << '\n';
};
}
int main()
{
auto sayName{ makeWalrus("Roofus") };
sayName();
return 0;
}
Example 2: cpp lambda function
#include <iostream>
using namespace std;
bool isGreater = [](int a, int b){ return a > b; }
int main() {
cout << isGreater(5, 2) << endl;
return 0;
}
Example 3: cpp lambda
struct X {
int x, y;
int operator()(int);
void f()
{
[=]()->int
{
return operator()(this->x + y);
};
}
};