c++ lambda表达式 C++11
1 #include <iostream> 2 #include <functional> 3 #include <string> 4 5 6 void fun1(std::string i,std::function<int(std::string& y)> send) 7 { 8 9 std::cout << "fun1:" << std::endl; 10 std::string yy = "fun2"; 11 send(yy); 12 } 13 14 int main() 15 { 16 17 fun1("123",[&](std::string& y){ 18 19 std::cout << "fun2:"<< y << std::endl; 20 return 0; 21 }); 22 23 int a = 123; 24 auto f = [&]{std::cout << a << std::endl;}; 25 f(); 26 a = 234; 27 f(); 28 getchar(); 29 return 0; 30 }
输出: g++ -std=c++11 main.cpp
1 fun1: 2 fun2:fun2 3 123 4 234
第24行
auto f = [&]{std::cout << a << std::endl;};换成
auto f = [=]{std::cout << a << std::endl;};
则输出为 123 123
参考博客 https://www.cnblogs.com/DswCnblog/p/5629165.html