一个lambda表达式表示一个可调用的代码单元。我们可以将其理解为一个未命名的内敛函数。一个lambda具有一个返回类型、一个参数列表和一个函数体。
一个lambda表达式具有如下形式
[capture list](parameter list)->return type {function body}
auto f = [] {return 42; };
cout << f() << endl;
向lambda传递参数
auto f2 = [](const string& a, const string& b)
{
return a+b;
};
string a1 = "Hello";
string a2 = "World";
cout << f2(a1, a2) << endl;
使用捕获列表
string pinjie(string str);
int main()
{
cout << pinjie("HaHa") << endl;
}
string pinjie(string str)
{
auto last = [str](const string& a, const string& b)
{
return str + " " + a + " " + b;
};
string res = last("Hello","World");
return res;
}
1441

被折叠的 条评论
为什么被折叠?



