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 }
相关文章
相关标签/搜索