【C++11lambda表达式】
mutable 修饰符,用于修改[]中以值传递的变量,无mutable修饰符的话则不行。
使用示例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <vector> 2 #include <iostream> 3 #include <algorithm> 4 #include <functional> 5 6 int main() 7 { 8 std::vector<int> c { 1,2,3,4,5,6,7 }; 9 int x = 5; 10 c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end()); 11 12 std::cout << "c: "; 13 for (auto i: c) { 14 std::cout << i << ' '; 15 } 16 std::cout << '\n'; 17 18 // the type of a closure cannot be named, but can be inferred with auto 19 auto func1 = [](int i) { return i+4; }; 20 std::cout << "func1: " << func1(6) << '\n'; 21 22 // like all callable objects, closures can be captured in std::function 23 // (this may incur unnecessary overhead) 24 std::function<int(int)> func2 = [](int i) { return i+4; }; 25 std::cout << "func2: " << func2(6) << '\n'; 26 }
【另一种函数语法】
这种语法也能套用到一般的函数定义与声明:
参考:
1、http://www.tuicool.com/articles/MjaaQ3