-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.cpp
53 lines (31 loc) · 1.48 KB
/
lambda.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int foo(int& a) {return ++a;}
int main()
{
auto f = [] (int a) -> int { return a + 1;};
cout << f(1) << endl;
//lambda表达式的__FUNCTION__是operator(),可以看出,lambda的实现上,其实也是一个类,重载了operator()
auto ff = [] (int a) -> int {cout << __FUNCTION__ << endl; return a + 1;};
cout << ff(2) << endl;
//没有参数时,可以省略参数符号
auto f1 = []() -> string { return "hello";};
cout << f1() << endl;
//对于返回值类型明显的函数,可省略返回值类型,甚至参数括号
auto f2 = []() { return "hello";};
cout << f2() << endl;
auto f3 = []{ return "hello";};
cout << f3() << endl;
int a = 1, b = 2;
auto f4 = [&]{return ++a;};
cout << "f4():" << f4() << ", a:" << a << endl; //注意!!!流的执行,是按从右到左来的,这里输出为2,1
//cout << "foo:" << foo(a) << ", a:" << a << endl;
cout << "a:" << a << endl;
//auto f5 = [=]{return ++a;}; //error,=捕获的变量,是readonly类型,即使在lambda内部复制了一份,也不能修改,修改了也不会影响外部的变量.但如果内部实在想修改,可以加mutable关键字,如auto f5 = [=]() mutable {return ++a;};
auto f6 = [=]{return b;};
++b;
cout << "f6():" << f6() << endl; //lambda捕获的值,在捕获那一该就定了,后面对b的修改,不影响其值.如果希望lambda即时捕获外面的参数值, 可以用引用方式捕获
return 0;
}