1 lambda[]
2 lambda[=]
3 lambda[&]
1 lambda[]
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使用
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”,因为尚未指定默认捕获模式
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[=]
按照副本引用this,还有当前块语句局部变量,不可以赋值,但是可以读取
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 中修改通过复制捕获
[=]可以读,不可以写
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 }