C++ Lambda表达式基本用法
創建一個匿名函數并執行。采用的是配對的方括號[]。實例如下:
| 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表达式基本用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: saphana服务器硬件评估,华为SAP
- 下一篇: 7700装win7