C++ Lambda表達式基本用法(言簡意賅,很是清楚)

建立一個匿名函數並執行。Objective-C採用的是上尖號^,而C++ 11採用的是配對的方括號[]。實例以下:ios

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
 
int main()
{
     []{
         cout << "Hello,Worldn" ;
     }();
}

咱們也能夠方便的將這個建立的匿名函數賦值出來調用:ide

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 個參數的函數wordpress

g++ 報錯:
error: ‘i’ is not captured函數

要直接沿用外部的變量須要在 [] 中指名捕獲。this

[=] 拷貝捕獲

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();
}

結果:
1024spa

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:0x28ff04code

[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/
相關文章
相關標籤/搜索