#include<iostream>
using namespace std;
//定义lambda
auto f = [] {cout << "hello lambda" << endl; };
void lambda()
{
int a = 4;
int b = 3;
//若使用[=]捕获时不能对a,b修改
[&]
{
++a;
++b;
}();
cout << a << ends << b << endl;
}
void mutlam()
{
int a = 3;
int b = 6;
//通过mutlab可以改变[=]的值但只在内部有效
auto f = [=]()mutable
{
++a;
++b;
cout << a << ends << b << endl;
};
f();
cout << a << ends << b << endl;
a = b = 25;
f();
cout << a << ends << b << endl;
}
int main()
{
//使用lambda
f();
[] {cout << "hello everyone!" << endl; }();
lambda();
mutlam();
system("pause");
return 0;
}
c++ lambda
最新推荐文章于 2023-08-21 10:29:06 发布