C++11中的lambda表达式是什么?我什么时候用?他们解决了哪类在介绍之前不可能解决的问题?
一些示例和用例将是有用的。
C++11中的lambda表达式是什么?我什么时候用?他们解决了哪类在介绍之前不可能解决的问题?
一些示例和用例将是有用的。
当前回答
什么是lambda函数?
lambda函数的C++概念起源于lambda演算和函数编程。lambda是一个未命名的函数,对于不可能重用且不值得命名的短代码片段非常有用(在实际编程中,而不是理论上)。
在C++中,lambda函数的定义如下
[]() { } // barebone lambda
或在它所有的荣耀
[]() mutable -> T { } // T is the return type, still lacking throw()
[]是捕获列表,()是参数列表,{}是函数体。
捕获列表
捕获列表定义了lambda外部的哪些内容应该在函数体内部可用,以及如何使用。它可以是:
a值:[x]参考[&x]通过引用[&]当前作用域中的任何变量与3相同,但按值[=]
您可以在逗号分隔的列表[x,&y]中混合上述任何一项。
参数列表
参数列表与任何其他C++函数中的参数列表相同。
功能体
实际调用lambda时将执行的代码。
退货类型扣除
如果lambda只有一个return语句,则可以省略返回类型,并具有decltype(return_statement)的隐式类型。
可变的
如果lambda标记为可变(例如[]()mutable{}),则允许对值捕获的值进行变异。
用例
ISO标准定义的库从lambdas中受益匪浅,并将可用性提高了几级,因为现在用户不必在某些可访问范围内使用小函子来扰乱代码。
C++14
在C++14中,各种提议扩展了lambdas。
已初始化Lambda捕获
捕获列表的元素现在可以用=初始化。这允许重命名变量并通过移动进行捕获。标准示例:
int x = 4;
auto y = [&r = x, x = x+1]()->int {
r += 2;
return x+2;
}(); // Updates ::x to 6, and initializes y to 7.
还有一张来自维基百科,展示了如何使用std::move:
auto ptr = std::make_unique<int>(10); // See below for std::make_unique
auto lambda = [ptr = std::move(ptr)] {return *ptr;};
通用Lambdas
Lambdas现在可以是通用的(如果T是周围范围中的某个类型模板参数):
auto lambda = [](auto x, auto y) {return x + y;};
改进的回报类型扣除
C++14允许为每个函数推导返回类型,并且不将其限制为返回表达式形式的函数;。这也扩展到lambdas。
其他回答
lambda函数是您在线创建的匿名函数。它可以捕获一些已经解释过的变量(例如。http://www.stroustrup.com/C++11FAQ.html#lambda),但存在一些限制。例如,如果有这样的回调接口,
void apply(void (*f)(int)) {
f(10);
f(20);
f(30);
}
您可以当场编写一个函数来使用它,就像下面传递给应用程序的函数一样:
int col=0;
void output() {
apply([](int data) {
cout << data << ((++col % 10) ? ' ' : '\n');
});
}
但你不能这样做:
void output(int n) {
int col=0;
apply([&col,n](int data) {
cout << data << ((++col % 10) ? ' ' : '\n');
});
}
由于C++11标准的限制。如果您想使用捕获,则必须依赖库和
#include <functional>
(或其他类似STL库的算法来间接获取),然后使用std::function,而不是像这样传递普通函数作为参数:
#include <functional>
void apply(std::function<void(int)> f) {
f(10);
f(20);
f(30);
}
void output(int width) {
int col;
apply([width,&col](int data) {
cout << data << ((++col % width) ? ' ' : '\n');
});
}
什么是lambda函数?
lambda函数的C++概念起源于lambda演算和函数编程。lambda是一个未命名的函数,对于不可能重用且不值得命名的短代码片段非常有用(在实际编程中,而不是理论上)。
在C++中,lambda函数的定义如下
[]() { } // barebone lambda
或在它所有的荣耀
[]() mutable -> T { } // T is the return type, still lacking throw()
[]是捕获列表,()是参数列表,{}是函数体。
捕获列表
捕获列表定义了lambda外部的哪些内容应该在函数体内部可用,以及如何使用。它可以是:
a值:[x]参考[&x]通过引用[&]当前作用域中的任何变量与3相同,但按值[=]
您可以在逗号分隔的列表[x,&y]中混合上述任何一项。
参数列表
参数列表与任何其他C++函数中的参数列表相同。
功能体
实际调用lambda时将执行的代码。
退货类型扣除
如果lambda只有一个return语句,则可以省略返回类型,并具有decltype(return_statement)的隐式类型。
可变的
如果lambda标记为可变(例如[]()mutable{}),则允许对值捕获的值进行变异。
用例
ISO标准定义的库从lambdas中受益匪浅,并将可用性提高了几级,因为现在用户不必在某些可访问范围内使用小函子来扰乱代码。
C++14
在C++14中,各种提议扩展了lambdas。
已初始化Lambda捕获
捕获列表的元素现在可以用=初始化。这允许重命名变量并通过移动进行捕获。标准示例:
int x = 4;
auto y = [&r = x, x = x+1]()->int {
r += 2;
return x+2;
}(); // Updates ::x to 6, and initializes y to 7.
还有一张来自维基百科,展示了如何使用std::move:
auto ptr = std::make_unique<int>(10); // See below for std::make_unique
auto lambda = [ptr = std::move(ptr)] {return *ptr;};
通用Lambdas
Lambdas现在可以是通用的(如果T是周围范围中的某个类型模板参数):
auto lambda = [](auto x, auto y) {return x + y;};
改进的回报类型扣除
C++14允许为每个函数推导返回类型,并且不将其限制为返回表达式形式的函数;。这也扩展到lambdas。
它解决了一个问题:对于使用输出参数函数初始化常量成员的构造函数调用,代码比lambda简单
您可以通过调用一个函数来初始化类的常量成员,该函数通过将其输出作为输出参数返回来设置其值。
C++11引入了lambda表达式,允许我们编写一个内联函数,该函数可用于短代码片段
[ capture clause ] (parameters) -> return-type
{
definition of method
}
通常,lambda表达式中的返回类型是由编译器自身计算的,我们不需要指定显式和->返回类型部分可以忽略,但在某些复杂情况下,如条件语句中,编译器无法确定返回类型,我们需要指定。
// C++ program to demonstrate lambda expression in C++
#include <bits/stdc++.h>
using namespace std;
// Function to print vector
void printVector(vector<int> v)
{
// lambda expression to print vector
for_each(v.begin(), v.end(), [](int i)
{
std::cout << i << " ";
});
cout << endl;
}
int main()
{
vector<int> v {4, 1, 3, 5, 2, 3, 1, 7};
printVector(v);
// below snippet find first number greater than 4
// find_if searches for an element for which
// function(third argument) returns true
vector<int>:: iterator p = find_if(v.begin(), v.end(), [](int i)
{
return i > 4;
});
cout << "First number greater than 4 is : " << *p << endl;
// function to sort vector, lambda expression is for sorting in
// non-decreasing order Compiler can make out return type as
// bool, but shown here just for explanation
sort(v.begin(), v.end(), [](const int& a, const int& b) -> bool
{
return a > b;
});
printVector(v);
// function to count numbers greater than or equal to 5
int count_5 = count_if(v.begin(), v.end(), [](int a)
{
return (a >= 5);
});
cout << "The number of elements greater than or equal to 5 is : "
<< count_5 << endl;
// function for removing duplicate element (after sorting all
// duplicate comes together)
p = unique(v.begin(), v.end(), [](int a, int b)
{
return a == b;
});
// resizing vector to make size equal to total different number
v.resize(distance(v.begin(), p));
printVector(v);
// accumulate function accumulate the container on the basis of
// function provided as third argument
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int f = accumulate(arr, arr + 10, 1, [](int i, int j)
{
return i * j;
});
cout << "Factorial of 10 is : " << f << endl;
// We can also access function by storing this into variable
auto square = [](int i)
{
return i * i;
};
cout << "Square of 5 is : " << square(5) << endl;
}
输出
4 1 3 5 2 3 1 7
First number greater than 4 is : 5
7 5 4 3 3 2 1 1
The number of elements greater than or equal to 5 is : 2
7 5 4 3 2 1
Factorial of 10 is : 3628800
Square of 5 is : 25
通过从封闭范围访问变量,lambda表达式可以比普通函数更强大。我们可以通过三种方式从封闭范围中捕获外部变量:
通过引用捕获按价值捕获两者都捕获(混合捕获)
用于捕获变量的语法:
[&]:通过引用捕获所有外部变量[=]:按值捕获所有外部变量[a,&b]:通过值捕获a,通过引用捕获b带有空捕获子句[]的lambda只能访问其本地变量。
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v1 = {3, 1, 7, 9};
vector<int> v2 = {10, 2, 7, 16, 9};
// access v1 and v2 by reference
auto pushinto = [&] (int m)
{
v1.push_back(m);
v2.push_back(m);
};
// it pushes 20 in both v1 and v2
pushinto(20);
// access v1 by copy
[v1]()
{
for (auto p = v1.begin(); p != v1.end(); p++)
{
cout << *p << " ";
}
};
int N = 5;
// below snippet find first number greater than N
// [N] denotes, can access only N by value
vector<int>:: iterator p = find_if(v1.begin(), v1.end(), [N](int i)
{
return i > N;
});
cout << "First number greater than 5 is : " << *p << endl;
// function to count numbers greater than or equal to N
// [=] denotes, can access all variable
int count_N = count_if(v1.begin(), v1.end(), [=](int a)
{
return (a >= N);
});
cout << "The number of elements greater than or equal to 5 is : "
<< count_N << endl;
}
输出:
First number greater than 5 is : 7
The number of elements greater than or equal to 5 is : 3
c++中的lambda被视为“随时可用的函数”。是的,你可以定义它;使用它;当父函数作用域结束时,lambda函数就消失了。
c++在c++11中引入了它,每个人都开始在每个可能的地方使用它。示例和lambda是什么可以在这里找到https://en.cppreference.com/w/cpp/language/lambda
我将描述每个c++程序员都必须知道的,但不存在哪些
Lambda并不意味着要在任何地方使用,每个函数都不能用Lambda替换。与正常功能相比,它也不是最快的。因为它有一些需要由lambda处理的开销。
在某些情况下,它肯定有助于减少线路数量。它基本上可以用于一段代码,这段代码在同一个函数中被调用一次或多次,在其他任何地方都不需要这段代码,因此您可以为它创建独立的函数。
下面是lambda的基本示例以及在后台发生的情况。
用户代码:
int main()
{
// Lambda & auto
int member=10;
auto endGame = [=](int a, int b){ return a+b+member;};
endGame(4,5);
return 0;
}
compile如何扩展它:
int main()
{
int member = 10;
class __lambda_6_18
{
int member;
public:
inline /*constexpr */ int operator()(int a, int b) const
{
return a + b + member;
}
public: __lambda_6_18(int _member)
: member{_member}
{}
};
__lambda_6_18 endGame = __lambda_6_18{member};
endGame.operator()(4, 5);
return 0;
}
正如您所看到的,当您使用它时,它会增加什么样的开销。所以在任何地方使用它们都不是好主意。它可以在适用的地方使用。