C ++ 14中的广义Lambda表达式
在C++11中,引入了lambda。Lambda基本是代码的一部分,可以嵌套在其他函数调用语句中。通过将lambda表达式与auto关键字结合使用,可以在以后使用它们。
在C++14中,这些lambda表达式得到了改进。在这里我们可以得到广义的λ。例如,如果我们要创建一个可以添加整数,添加数字以及连接字符串的lambda,则必须使用此广义lambda。
lambda表达式的语法如下所示:
[](auto x, auto y) { return x + y; }让我们看一个例子,以获得更好的主意。
示例
#include <iostream>
#include <string>
using namespace std;
main() {
auto add = [](auto arg1, auto arg2) { //define generalized lambda
return arg1 + arg2;
};
cout << "Sum of integers: " << add(5, 8) << endl;
cout << "Sum of floats: " << add(2.75, 5.639) << endl;
cout << "Concatenate Strings: " << add(string("Hello "), string("World")) << endl;
}输出结果
Sum of integers: 13 Sum of floats: 8.389 Concatenate Strings: Hello World
热门推荐
10 朋友搬迁新房祝福语简短
11 冬季祝福语大全 简短的
12 化学老师元旦祝福语简短
13 新年送老师简短祝福语
14 老师高考后祝福语 简短
15 上司生日祝福语简短 女性
16 简短的高考祝福语精选
17 生日祝福语简短蛋糕上
18 幼儿老师过年简短祝福语