xxxxxxxxxx
struct S2 { void f(int i); };
void S2::f(int i)
{
[=]{}; // OK: by-copy capture default
[=, &i]{}; // OK: by-copy capture, except i is captured by reference
[=, *this]{}; // until C++17: Error: invalid syntax
// since C++17: OK: captures the enclosing S2 by copy
[=, this] {}; // until C++20: Error: this when = is the default
// since C++20: OK, same as [=]
}
xxxxxxxxxx
- Good website to learn lambda in c++:
https://docs.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp
https://en.cppreference.com/w/cpp/language/lambda
https://www.geeksforgeeks.org/lambda-expression-in-c/
https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11
https://linuxhint.com/lambda-expressions-in-c/
xxxxxxxxxx
//lambda function for sorting vector ascending
sort(vec.begin(),vec.end(),[](int &a, int &b){
return a<b;
});
//lambda function for sorting vector of vector ascending based on first value
sort(vec.begin(),vec.end(),[](vector<int> &a, vector<int> &b){
return a[0]<b[0];
});