1- Good website to learn lambda in c++:
2https://docs.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp
3https://en.cppreference.com/w/cpp/language/lambda
4https://www.geeksforgeeks.org/lambda-expression-in-c/
5https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11
6https://linuxhint.com/lambda-expressions-in-c/
1#include <iostream>
2using namespace std;
3
4bool isGreater = [](int a, int b){ return a > b; }
5
6int main() {
7 cout << isGreater(5, 2) << endl; // Output: 1
8 return 0;
9}
10
1#include <iostream>
2#include <string>
3
4// returns a lambda
5auto makeWalrus(const std::string& name)
6{
7 // Capture name by reference and return the lambda.
8 return [&]() {
9 std::cout << "I am a walrus, my name is " << name << '\n'; // Undefined behavior
10 };
11}
12
13int main()
14{
15 // Create a new walrus whose name is Roofus.
16 // sayName is the lambda returned by makeWalrus.
17 auto sayName{ makeWalrus("Roofus") };
18
19 // Call the lambda function that makeWalrus returned.
20 sayName();
21
22 return 0;
23}
24
1struct X {
2 int x, y;
3 int operator()(int);
4 void f()
5 {
6 // the context of the following lambda is the member function X::f
7 [=]()->int
8 {
9 return operator()(this->x + y); // X::operator()(this->x + (*this).y)
10 // this has type X*
11 };
12 }
13};