C++ lambda 表达式
<h2 id="1-什么是lambda表达式">1 什么是lambda表达式?</h2><p>lambda表达式实际上<strong>就是一个函数对象</strong>。和函数一样可以使用圆括号调用。</p>
<p>只不过这个函数可以定义在代码的任意位置。非常的<strong>灵活,自由</strong>。</p>
<p>lambda表达式比函数更具有优势的地方在于,<strong>它可以将当前作用域中的变量,对象直接拿来使用</strong>(灵活)。</p>
<p>示例1:求余数</p>
<pre><code class="language-c++">#include <iostream>
using namespace std;
int main()
{
//定义一个lambda表达式对象mod,用于对两个数求余
auto mod = [](int a, int b) { return a % b; };
cout << mod(3, 7) << endl;// 3%7=3
cout << mod(7, 3) << endl;// 7%3=1
cout << mod(3, 3) << endl;// 3%3=0
cout << mod(7, 7) << endl;// 7%7=0
return 0;
}
</code></pre>
<h2 id="2-lambda表达式的定义">2 lambda表达式的定义</h2>
<pre><code> (parameter list) -> return type { function body}
</code></pre>
<p>其中,各个部分的含义如下:</p>
<p><strong>1) capture list</strong></p>
<p>捕获列表,可以写当前作用域中的变量名,这些变量就可以在lambda表达式内直接使用了。如果为空,表示不需要使用当前作用域中的变量。</p>
<p><strong>2) return type</strong></p>
<p>函数(lambda表达式)返回值类型;</p>
<p>可忽略不写(就像上面的求余数mod),因为编译器会自动推导返回值类型;</p>
<p><strong>3) parameter list</strong></p>
<p>参数列表,这个跟普通函数的参数列表是一样的,表示形参列表。</p>
<p><strong>4) function body</strong></p>
<p>函数体,和普通函数的函数体一样,里面写你的函数逻辑,可以调用[]和()里的参数</p>
<h2 id="3-捕获列表">3 捕获列表</h2>
<ul>
<li>[]没有任何参数,这种情况下不传入外部参数。</li>
<li> 传入变量a的值以及变量b的引用。</li>
<li>[&] 以引用方式传入所有变量。</li>
<li>[=] 以传值方式传入所有变量,值不可被修改。</li>
<li>[&, a] 除了a用传值方式传入,其他所有变量用传值方式传入。</li>
<li>[=, &a] 除了a用引用方式传入,其他所有变量用引用方式传入。</li>
</ul>
<p><strong>1) 值捕获</strong></p>
<p>lambda表达式默认使用值捕获,等于是拿到当前作用域中变量的副本(值)。</p>
<pre><code>auto add = (int a, int b) { return start + a + b; };
</code></pre>
<p>完整示例:</p>
<pre><code class="language-c++">#include <iostream>
using namespace std;
int main()
{
int start = 100;
//值捕获start变量
auto add = (int a, int b) { return start + a + b; };
cout << add(20, 3) << endl;// 100+20+3=123
return 0;
}
</code></pre>
<p>输出:123</p>
<p><strong>2) 引用捕获</strong></p>
<p>引用捕获需要加上引用符号。</p>
<pre><code>auto add = [&sum](int a, int b) { sum = a + b; };
</code></pre>
<p>完整示例:</p>
<pre><code class="language-c++">#include <iostream>
using namespace std;
int main()
{
int sum = 0;
//引用捕获sum变量
auto add = [&sum](int a, int b) { sum = a + b; };
add(20, 3);// sum = 20+3 = 23
cout << sum << endl;// 23
return 0;
}
</code></pre>
<p>输出:23</p>
<p><strong>3) 值捕获全部</strong></p>
<p>使用 = 号表示按值捕获当前作用域中的所有变量(不常用)</p>
<pre><code>auto add = [=](int a, int b) { sum = a + b; };
</code></pre>
<p><strong>4) 引用捕获全部</strong></p>
<p>使用& 号来按引用捕获当前作用域中的所有变量(不常用)</p>
<pre><code> auto add = [&](int a, int b) { sum = a + b; };
</code></pre>
<h2 id="4-lambda表达式的应用">4 lambda表达式的应用</h2>
<p>下面我们给std::sort传递一个lambda表达式来实现升序排序</p>
<pre><code class="language-c++">#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> arr{2, 7, 1, 8, 2, 8};
//按升序排序sort 需要一个 函数bool fun(int a, int b);
std::sort(arr.begin(), arr.end(), [](int a, int b) { return a < b; });
for (auto i : arr)
{
cout << i << " ";
}
return 0;
}
</code></pre>
<p>程序输出:</p>
<pre><code>1 2 2 7 8 8
</code></pre>
<h2 id="保存lambda函数">保存lambda函数</h2>
<p>使用std::function可以存储lambda函数。例如,可以用function<void()>来存放fun0,function<int()>来存放fun1,带参数的函数可以在()内输入参数类型,在使用 function 时要包含头文件functional。</p>
<pre><code class="language-c++">int a, b, c;
auto fun0 = [&]() -> void { a = 1; b = 2; c = 3; };
auto fun1 = [=]() -> int { return 2 * 3; };
auto fun2 = [=, &a, &b]() -> void { ++a; b += c + a; };
auto fun3 = [=]() -> int { return a + c; };
//a、b、c 分别为1、2、3
fun0();
//c = 6
c = fun1();
//a = 2 b = 858993456 c = 6
fun2();
//b = 1717986916
b = fun3();
#include <functional>
function<void()> f1 = fun0;
function<int()> f2 = fun1;
</code></pre>
<h2 id="参考资料">参考资料</h2>
<p>C++ lambda 表达式 - 知乎 (zhihu.com)</p><br><br>
来源:https://www.cnblogs.com/zhaoqingqing/p/18919605
頁:
[1]