创建一个匿名函数并执行。采用的是配对的方括号[]。实例如下:

1

2

3

4

5

6

7

8

9

#include <iostream>

using namespace std;

int main()

{

    []{

        cout << "Hello,Worldn";

    }();

}

我们也可以方便的将这个创建的匿名函数赋值出来调用:

1

2

3

4

5

6

7

8

9

10

11

#include <iostream>

using namespace std;

int main()

{

    int i = 1024;

    auto func = [](int i) { // (int i) 是指传入改匿名函数的参数

        cout << i;

    };

    func(i);

}

捕获选项

  • [] Capture nothing (or, a scorched earth strategy?)
  • [&] Capture any referenced variable by reference
  • [=] Capture any referenced variable by making a copy
  • [=, &foo] Capture any referenced variable by making a copy, but capture variable foo by reference
  • [bar] Capture bar by making a copy; don’t copy anything else
  • [this] Capture the this pointer of the enclosing class

[] 不捕获任何变量

1

2

3

4

5

6

7

8

9

#include <iostream>

using namespace std;

int main()

{

    int i = 1024;

    auto func = [] { cout << i; };

    func();

}

vs 报错
error C3493: 无法隐式捕获“i”,因为尚未指定默认捕获模式
error C2064: 项不会计算为接受 0 个参数的函数

g++ 报错:
error: ‘i’ is not captured

要直接沿用外部的变量需要在 [] 中指名捕获。

[=] 拷贝捕获

1

2

3

4

5

6

7

8

9

10

11

#include <iostream>

using namespace std;

int main()

{

    int i = 1024;

    auto func = [=]{  // [=] 表明将外部的所有变量拷贝一份到该函数内部

        cout << i;

    };

    func();

}

结果:
1024

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include <iostream>

using namespace std;

int main()

{

    int i = 1024;

    auto fun1 = [=]{

        // fun1 内存在 i

        cout << i; // 1024

        auto fun2 = []{ // 未指名捕获, i 不存在

            cout << i;

        };

        fun2();

    };

    fun1();

}

[&] 引用捕获

1

2

3

4

5

6

7

8

9

10

11

12

#include <iostream>

using namespace std;

int main()

{

    int i = 1024;

    cout << &i << endl;

    auto fun1 = [&]{

        cout << &i << endl;

    };

    fun1();

}

结果:
0x28ff0c
0x28ff0c

[=, &] 拷贝与引用混合

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include <iostream>

using namespace std;

int main()

{

    int i = 1024, j = 2048;

    cout << "i:" << &i << endl;

    cout << "j:" << &j << endl;

    auto fun1 = [=, &i]{ // 默认拷贝外部所有变量,但引用变量 i

        cout << "i:" << &i << endl;

        cout << "j:" << &j << endl;

    };

    fun1();

}

1

结果
outside i:0x28ff0c
outside j:0x28ff08
inside i:0x28ff0c
inside j:0x28ff04

[bar] 指定引用或拷贝

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#include <iostream>

using namespace std;

int main()

{

    int i = 1024, j = 2048;

    cout << "outside i value:" << i << " addr:" << &i << endl;

    auto fun1 = [i]{

        cout << "inside  i value:" << i << " addr:" << &i << endl;

        // cout << j << endl; // j 未捕获

    };

    fun1();

}

结果:
outside i value:1024 addr:0x28ff08
inside i value:1024 addr:0x28ff04

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#include <iostream>

using namespace std;

int main()

{

    int i = 1024, j = 2048;

    cout << "outside i value:" << i << " addr:" << &i << endl;

    auto fun1 = [&i]{

        cout << "inside  i value:" << i << " addr:" << &i << endl;

        // cout << j << endl; // j 未捕获

    };

    fun1();

}

结果:
outside i value:1024 addr:0x28ff08
inside i value:1024 addr:0x28ff08

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#include <iostream>

using namespace std;

int main()

{

    int i = 1024, j = 2048, k;

    cout << "outside i:" << &i << endl;

    cout << "outside j:" << &j << endl;

    auto fun1 = [i, &j]{

        cout << "inside  i:" << &i << endl;

        cout << "inside  j:" << &j << endl;

        // cout << k; // k 未捕获

    };

    fun1();

}

结果:
outside i:0x28ff0c
outside j:0x28ff08
inside i:0x28ff00
inside j:0x28ff08

[this] 捕获 this 指针

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#include <iostream>

using namespace std;

class test

{

public:

    void hello() {

        cout << "test hello!n";

    };

    void lambda() {

        auto fun = [this]{ // 捕获了 this 指针

            this->hello(); // 这里 this 调用的就是 class test 的对象了

        };

        fun();

    }

};

int main()

{

    test t;

    t.lambda();

}

C++ Lambda表达式基本用法相关推荐

  1. python -lambda表达式的用法

    匿名函数 lambda的意义: 利用lambda我们可以速写函数,不用去定义函数就可以直接使用 y = lambda x,z:5+9*x+8*z print(y(2,4)) 从例子中可以看到,lamb ...

  2. python lambda表达式及用法_Python:lambda表达式和yield关键字理解与使用讲解

    一.lambda表达式 1.1.lambda表达式理解 lambda的主体是一个表达式,而不是一个代码块,仅仅能在lambda表达式中封装有限的逻辑进去.如果要通俗的理解lambda表达式,可以结合C ...

  3. Python lambda表达式及用法

    lambda 表达式是现代编程语言争相引入的一种语法,如果说函数是命名的.方便复用的代码块,那么 lambda 表达式则是功能更灵活的代码块,它可以在程序中被传递和调用. 回顾局部函数 回顾<P ...

  4. python lambda表达式及用法_python lambda表达式简单用法

    条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: # 普通条件语句 if 1 == 1: name = 'wupeiqi' else: name = 'alex' # 三元运 ...

  5. Python lambda表达式用法

    python lambda表达式简单用法 lambda表达式简单用法 lambda是什么? lambda表达式在sort函数中的使用 内置函数 lambda是什么? 看个例子: g = lambda ...

  6. python lambda表达式的使用方法(匿名函数)

    文章目录 lambda函数简介 示例1:将存有文件名的字符串列表,运用正则表达式方法提取文件名中的数字,按照数字大小将文件名字符串列表重新排序 示例2:将存有hsv颜色空间的颜色元组列表,转换成rgb ...

  7. java1.8 lambda表达式_java1.8之Lambda表达式

    行为参数化 为了应对多变的需求,难道我们就要因为客户每提出一个需求,我们就要写一个方法去实现吗? 显然这样做很冗余,而且维护性大大降低,这说明代码的设计不够好.好在已经有前人帮我们提出了行为参数化思想 ...

  8. lambda函数if_现代 C++:Lambda 表达式

    Lambda 表达式(Lambda Expression)是 C++11 引入的一个"语法糖",可以方便快捷地创建一个"函数对象". 从 C++11 开始,C+ ...

  9. C++11特性《 右值引用-<完美转发>、lambda表达式》

    1.右值引用 1.1移动语义 如果一个类中涉及到资源管理,用户必须显式提供拷贝构造.赋值运算符重载以及析构函数,否则编译器将 会自动生成一个默认的,如果遇到拷贝对象或者对象之间相互赋值,就会出错,比如 ...

最新文章

  1. 一次excel的countifs的成功应用
  2. JavaScript中十种一步拷贝数组的方法
  3. realloc函数在使用上要注意什么问题
  4. 华为交换机eth口作用_华为交换机口如何绑定端口号
  5. 来自于51CTO的经典学习资料汇总
  6. android stadio 编译报错:download fastutil-7.2.0.jar
  7. 航空公司VIP客户查询(25 分)(Hash)
  8. SM4算法 C语言 (从OpenSSL库中分离算法:七)
  9. 省会、自治区、直辖市、特别行政区
  10. 极大似然法python例子
  11. 静默安装oracle11,Oracle11g静默安装
  12. 【Verilog基础】卡诺图化简要点总结
  13. 如何理解惯性问题,是物理学的大问题
  14. c语言俩小时不挂科——全程高能,没有废话
  15. 51单片机入门学习------环境搭建
  16. 深度Linux安装火狐,deepin或Ubuntu安装最新版Firefox,并设置去掉标题栏
  17. adobe photoshop2021中文完整直装版
  18. 最大公约最小公倍数算法
  19. 怎么做好网络营销推广引流客户?
  20. JZ3 从头到尾打印链表

热门文章

  1. 从底层重学 Java 之 Stream 并行及标志 GitChat连接
  2. three.js script vertex和fragment在react中使用/纯js写法
  3. .net mysql-connector-net连接mysql
  4. (JAVA)Object类之toString()和equals()
  5. 【C++深度剖析教程10】C++中的字符串类
  6. WPF vs2015,vs2012 添加ArcObjects SDK
  7. 三羊献瑞(暴力破解)
  8. JS脚本病毒调试脚本-Trojan[Downloader]:JS/Nemucod
  9. WPF中的数据绑定Data Binding使用小结
  10. js学习总结----柯里化函数