C++11_lambda           

C++11 系列之 Lambda

原文

完整表达式

[ capture ] ( params ) mutable exception attribute -> ret { body }

例子

auto func1 = [](int i) { return i+4; };

解释:

例子:

 std::vector<int> c { 1,2,3,4,5,6,7 };
 int x = 5;
 c.erase(std::remove_if(c.begin(), 
                        c.end(), 
                        [x](int n) { return n < x; } ), // x 是 capture 到的外部变量  
                        c.end());
 
 std::cout << "c: ";
 for (auto i: c) {
     std::cout << i << ' ';
 }
 std::cout << '\n';
 
 [] {std::cout << "Hello from Lambda" << std::endl} (); // 最后的()让该 lambda 立即执行