lamda表达式定义了一个匿名函数,并且可以捕获一定范围的变量,其定义如下:
[capture](params) mutable->return_type{函数体}
(params)参数列表,若无参数 可以连同括号一起省略
例如在下面的例子中:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
int sum = 0;
std::for_each(v.begin(), v.end(), [&](int n) {
sum += n;
});
std::cout << "The sum is " << sum << std::endl;
return 0;
}
No comments yet.