Understanding C++: C++11 Lambda expressions

understanding

Reference link:

Lambda expressions are also called anonymous functions. They already exist in C# and python, and C++11 only started.

Statement format:

[capture list] (params list) mutable exception-> return type { function body }
其中:

  • capture list: capture a list of external variables
  • params list: formal parameter list
  • Mutable indicator: whether to modify the captured variable
  • exception: exception setting
  • return type: return type
  • function body: function body

Other formats:

  • [capture list] (params list) -> return type {function body}
  • [capture list] (params list) {function body}//The return type can be guessed based on return, if the return type is not guessed, it is void
  • [capture list] {function body}//Similar to a parameterless function

First try

(Used by sort):

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(int a, int b)
{
    return  a < b;
}
int main()
{
    vector<int> myvec{ 3, 2, 5, 7, 3, 2 };
    vector<int> lbvec(myvec);

    sort(myvec.begin(), myvec.end(), cmp); // 旧式做法
    cout << "predicate function:" << endl;
    for (int it : myvec)
        cout << it << ' ';
    cout << endl;

    sort(lbvec.begin(), lbvec.end(), [](int a, int b) -> bool { return a < b; });   // Lambda表达式
    cout << "lambda expression:" << endl;
    for (int it : lbvec)
        cout << it << ' ';
}

Different capture forms:

Value capture

The value cannot be changed after capture ; the value of the captured variable is passed in by value copy when the Lambda expression is created, so subsequent modifications to the variable will not affect the value in the Lambda expression .

#include <iostream>
using namespace std;

int main()
{
    int a = 123;
    auto f = [a] { cout << a << endl; };//不能改变lambda里面的a值
    a = 321;
    f(); // 输出:123
    return 0;
}

Reference capture

You can change its value after capturing, and at the same time, modifying the captured value later will also affect it

#include <iostream>
using namespace std;

int main()
{
    int a = 123;
    auto f = [&a] { cout << a << endl; };
    a = 321;
    f(); // 输出:321
}

Implicit capture:

The compiler infers which variables need to be captured based on the code in the function body. This method is called implicit capture. There are two ways of implicit capture, namely [=] and [&]. [=] means to capture external variables by value capture, [&] means to capture external variables by reference capture.

Implicit value capture:

#include <iostream>
using namespace std;

int main()
{
    int a = 123;
    auto f = [=] { cout << a << endl; };    // 隐式值捕获
    f(); // 输出:123
}

Implicit reference capture:

int main()
{
    int a = 123;
    auto f = [&] { cout << a << endl; };    // 隐式引用捕获
    a = 321;
    f(); // 输出:321
}

mutable: You can modify the value captured by the value:

#include <iostream>
using namespace std;

int main()
{
    int a = 123;
    auto f = [a]()mutable { cout << ++a; }; // 不会报错
    cout << a << endl; // 输出:123,值捕获,不影响后面的值
    f(); // 输出:124
}

Guess you like

Origin blog.csdn.net/QLeelq/article/details/111059206