lambda

 

1 lambda[]ios

2 lambda[=]函數

3 lambda[&]this

 

1 lambda[]spa

 

lambda帶參數的函數,和不帶參數的函數指針

 

 1 #include <iostream>
 2 
 3 void main()
 4 {
 5     auto fun1 = []() {std::cout << "hello china" << std::endl; };//fun1是函數指針,函數沒有參數
 6 
 7     fun1();//執行函數
 8 
 9     auto fun2 = [](int a, int b) {return a + b; };//fun2是函數指針,函數有參數
10 
11     std::cout << fun2(10, 9) << std::endl;
12 }

 

for_each搭配Lambda使用code

 

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 void main()
 6 {
 7     std::vector<int>myv;
 8 
 9     myv.push_back(1);
10     myv.push_back(2);
11     myv.push_back(11);
12 
13     auto fun = [](int v) {std::cout << v << std::endl; };//函數指針
14 
15     for_each(myv.begin(), myv.end(), fun);
16 }

 

error C3493: 沒法隱式捕獲「a」,由於還沒有指定默認捕獲模式blog

 

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 void main()
 6 {
 7     std::vector<int>myv;
 8 
 9     myv.push_back(1);
10     myv.push_back(2);
11     myv.push_back(11);
12 
13     int a = 10;
14 
15     auto fun = [](int v) {v += a; std::cout << v << std::endl; };//error C3493: 沒法隱式捕獲「a」,由於還沒有指定默認捕獲模式
16 
17     for_each(myv.begin(), myv.end(), fun);
18 }

 

2 lambda[=]it

 

按照副本引用this,還有當前塊語句局部變量,不能夠賦值,可是能夠讀取io

 

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 void main()
 6 {
 7     std::vector<int>myv;
 8 
 9     myv.push_back(1);
10     myv.push_back(2);
11     myv.push_back(11);
12 
13     int a = 10;
14 
15     auto fun = [=](int v) {v += a; std::cout << v << std::endl; };//加上=
16 
17     for_each(myv.begin(), myv.end(), fun);
18 }

 

error C3491: 「a」: 沒法在非可變 lambda 中修改經過複製捕獲table

 

[=]能夠讀,不能夠寫

 

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 void main()
 6 {
 7     std::vector<int>myv;
 8 
 9     myv.push_back(1);
10     myv.push_back(2);
11     myv.push_back(11);
12 
13     int a = 10;
14 
15     auto fun = [=](int v) {v += a; std::cout << v << std::endl; a = 3; };//error C3491: 「a」: 沒法在非可變 lambda 中修改經過複製捕獲
16 
17     for_each(myv.begin(), myv.end(), fun);
18 
19     std::cout << a << std::endl;
20 }

 

3 lambda[&]

 

&按照引用的方式操做局部變量,能夠賦值,能夠讀取

 

[&]引用所有變量

 

[&a]引用變量a

 

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 void main()
 6 {
 7     std::vector<int>myv;
 8 
 9     myv.push_back(1);
10     myv.push_back(2);
11     myv.push_back(11);
12 
13     int a = 10;
14 
15     auto fun = [&a](int v) {v += a; std::cout << v << std::endl; a = 3; };//[&a]引用變量
16 
17     for_each(myv.begin(), myv.end(), fun);
18 
19     std::cout << a << std::endl;
20 }

 

[]() {std::cout << "hello china"; };//函數指針

[]() {std::cout << "hello world"; }();//調用函數

 

1 #include <iostream>
2 
3 void main()
4 {
5     []() {std::cout << "hello china"; };//函數指針
6 
7     []() {std::cout << "hello world"; }();//調用函數
8 }

 

在類中使用lambda

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 
 5 class test
 6 {
 7 public:
 8     std::vector<int>myv;
 9     int num;
10 public:
11     test()
12     {
13         num = 12;
14         myv.push_back(10);
15         myv.push_back(11);
16     }
17     void add()
18     {
19         int x = 3;
20         auto fun = [&](int v) {std::cout << v + x + this->num << std::endl; };
21         for_each(this->myv.begin(), this->myv.end(), fun);
22     }
23 };
24 
25 void main()
26 {
27     test a;
28 
29     a.add();
30 }

 

lambda的函數返回值

 

 1 #include <iostream>
 2 
 3 void main()
 4 {
 5     auto fun1 = []()->double {std::cout << "hello china" << std::endl; return 1; };
 6 
 7     std::cout << fun1() << std::endl;
 8 
 9     auto fun2 = [](int a, double b)->decltype(a / b) {std::cout << "hello world" << std::endl; return a / b; };
10 
11     std::cout << fun2(1, 2.3) << std::endl;
12 }

 

lambda和mutable

 

mutable,僅僅修改局部變量a

 

 1 #include <iostream>
 2 
 3 void main()
 4 {
 5     int a = 10;
 6 
 7     auto fun1 = [a](int v)mutable{ v += a; std::cout << v << std::endl; a = 3; };//mutable,僅僅修改局部變量a
 8 
 9     fun1(9999);
10 
11     std::cout << a << std::endl;//仍然是10
12 }
相關文章
相關標籤/搜索