参考网址:
https://blog.csdn.net/Dasis/article/details/121372235
https://blog.csdn.net/halazi100/article/details/122944457
我们有这样的例子:
int main()
{
int a = 1;
int b = 2;
auto f = [a, b](int c, int d)->int {
return a + b + c + d;
};
int e = f(3, 4);
std::vector some_list{ 1, 2, 3, 4, 5 };
int total = 0;
std::for_each(some_list.begin(), some_list.end(), [&total](const int x) {
total += x;
});
return 0;
}
lambda表达式中如果要使用函数体外的变量,则要使用捕获列表。
回调函数举例:
typedef int(*func)(int a, int b);
int show(func callback, int a, int b)
{
return callback(a, b);
}
int add(int a, int b)
{
return a + b;
}
int main()
{
int c = show(add, 1, 2);
return 0;
}



