C++ Lambda表达式基本用法(言简意赅,非常清楚)
2024-09-25 10:20:22
创建一个匿名函数并执行。Objective-C采用的是上尖号^,而C++ 11采用的是配对的方括号[]。实例如下:
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(); } |
https://lellansin.wordpress.com/2014/01/02/c-lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95/
最新文章
- 打包SpringBoot工程并部署
- Clock rate
- eclipse gradle插件(buildship)的安装和使用
- 会计凭证BAPI_ACC_DOCUMENT_POST
- 【关于服务器端SQL Server 2008的设置】 使其他客户端机可通过ODBC数据源可访问
- Codeforces Round #339 Div.2 B - Gena&#39;s Code
- 可变参数列表---以dbg()为例
- SWI-Prolog
- Quartz总结(三):动态修改定时器一
- android 点滴记录
- rt-thread 之网络组件
- windows 端口转发
- SQL 字段修改
- decorator 装饰页面,根据不同设备自动切换移动和pc站
- 潭州课堂25班:Ph201805201 django 项目 第二十五课 文章多级评论前后台实现 (课堂笔记)
- zabbix3.4.7利用Windows性能监视器监控各项资源指标
- Bean和Spirng模块
- ubuntu16.04之sudo问题
- eclipse中svn提交报错的解决
- JQuery EasyUI 日期控件 怎样做到只显示年月,而不显示日
热门文章
- C# 获取文件路径,读取项目中某程序集下文件
- docker基础(二)
- UVA 10603 - Fill BFS~
- Spring中@Async用法详解及简单实例
- [PostgreSQL] Use Foreign Keys to Ensure Data Integrity in Postgres
- [Phonegap+Sencha Touch] 移动开发72 List列表横向滑动操作(仿QQ列表滑动删除)
- js 省市二级联动
- angular自定义指令相关知识及代码
- (十)RabbitMQ消息队列-高可用集群部署实战
- iOS调试 - 基本技巧