2021-04-18

2021-04-18  本文已影响0人  瑜明青云

C++11新特性

Lambda表达式

int (*p)(int, int) = [](int a, int b) -> int {
        return a + b;
    };

    cout << p(10, 20) << endl;

cout << [](int a, int b) -> int {
        return a + b;
    }(10, 20) << endl;

cout << [](int a, int b) {
        return a + b;
    }(10, 20) << endl;
// 默认情况是值捕获
    auto p = [=, &a] {
        cout << a << endl;
        cout << b << endl;
    };

    a = 11;
    b = 22;

    p();

int a = 10;
    int b = 20;

    auto p = [a] () mutable {
        a++;
        cout << "lambda - " << a << endl;
    };
    p();
mutable 相当于a有个临时变量

C++ 17

if (int a = 10;  a > 5) {

    }
    else if (int b = 20; b > 10) {

    }
    else if (2) {

    }
    else {

    }
上一篇下一篇

猜你喜欢

热点阅读