【C++11lambda表达式】
mutable 修饰符,用于修改[]中以值传递的变量,无mutable修饰符的话则不行。
使用示例:
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional> int main()
{
std::vector<int> c { ,,,,,, };
int x = ;
c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end()); std::cout << "c: ";
for (auto i: c) {
std::cout << i << ' ';
}
std::cout << '\n'; // the type of a closure cannot be named, but can be inferred with auto
auto func1 = [](int i) { return i+; };
std::cout << "func1: " << func1() << '\n'; // like all callable objects, closures can be captured in std::function
// (this may incur unnecessary overhead)
std::function<int(int)> func2 = [](int i) { return i+; };
std::cout << "func2: " << func2() << '\n';
}
【另一种函数语法】
这种语法也能套用到一般的函数定义与声明:
参考: