C++ Lambda表达式基本用法(言简意赅,非常清楚)
创建一个匿名函数并执行。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();} |
C++ Lambda表达式基本用法(言简意赅,非常清楚)的更多相关文章
- python lambda表达式简单用法【转】
python lambda表达式简单用法 1.lambda是什么? 看个例子: g = lambda x:x+1 看一下执行的结果: g(1) >>>2 g(2) >>& ...
- C++11 中function和bind以及lambda 表达式的用法
关于std::function 的用法: 其实就可以理解成函数指针 1. 保存自由函数 void printA(int a) { cout<<a<<endl; } std:: ...
- Lambda 表达式递归用法实例
注意: 使用Lambda表达式会增加额外开销,但却有时候又蛮方便的. Windows下查找子孙窗口实例: HWND FindDescendantWindows(HWND hWndParent, LPC ...
- python lambda表达式简单用法
习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: 1 2 3 4 5 6 7 8 # 普通条件语句 if 1 == 1: name = 'wupeiqi' else ...
- 快速掌握Java中Lambda表达式的用法
Lambda表达式的作用: Lambda表达式的作用就是简化代码开发,让代码看起来更加简介.它是用来简化匿名内部类的.但是并不是所有的匿名内部类都能用Lambda表达式简化,Lambda表达式是有使用 ...
- Lambda表达式的用法
参考:https://www.cnblogs.com/knowledgesea/p/3163725.html
- c++中lambda表达式的用法
#include <iostream> using namespace std; int main(){ ; auto func1 = [=](;}; auto func2 = [& ...
- java8+ Lambda表达式基本用法
LIST public class LambdaTest { @Getter @Setter @AllArgsConstructor static class Student{ private Lon ...
- C# LINQ查询表达式用法对应Lambda表达式
C#编程语言非常优美,我个人还是非常赞同的.特别是在学习一段时间C#后发现确实在它的语法和美观度来说确实要比其它编程语言强一些(也可能是由于VS编译器的加持)用起来非常舒服,而且对于C#我觉得他最优美 ...
随机推荐
- hdu2049(组合数学)
题意:每位新娘打扮得差点儿一模一样,并盖上大大的红盖头随机坐成一排;然后,让各位新郎寻找自己的新娘.每人仅仅准找一个,而且不同意多人找一个.最后,揭开盖头,如果找错了对象就要当众跪搓衣板...如果一共 ...
- mangodb学习0.1 概念
摘自菜鸟教程
- 使用SystemC进行硬件仿真
使用SystemC进行硬件仿真 环境 linux-x86-64 bash g++ 下载解压SystemC SystemC下载地址 解压下载的包 tar zxvf systemc-2.3.3.tar.g ...
- [AngularFire2] Pagination
Let's see how to do pagination in Firebase: For the init loading, we only want 3 items: findLessonsK ...
- The behavior of App killed or restored by Android System or by users
What's the behavior of App killed or restored by Android System or by users? First, user kills the a ...
- go get请求 json字符串转为结构体
package main import ( "io/ioutil" "fmt" "net/http" "encoding/json ...
- ZOJ-1649 Rescue BFS (HDU 1242)
看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...
- Spring web 工具类 WebApplicationContextUtils
概述 Spring web 的工具类 WebApplicationContextUtils 位于包 org.springframework.web.context.support 是访问一个Servl ...
- JavaScript中的一些细节 分类: C1_HTML/JS/JQUERY 2014-08-05 16:45 384人阅读 评论(0) 收藏
1.设置id / class等属性 用 setAttribute 设置一些常规属性如 id ,className 的时候经常不起作用,只能用 object.id = value 这样来设置 news_ ...
- 【solr专题之二】配置文件:solr.xml solrConfig.xml schema.xml 分类: H4_SOLR/LUCENCE 2014-07-23 21:30 1959人阅读 评论(0) 收藏
1.关于默认搜索域 If you are using the Lucene query parser, queries that don't specify a field name will use ...