我是靠谱客的博主 现实红酒,这篇文章主要介绍C++11Lambda表达式捕获变量(Capturing the Variable)与本地变量(Local Variable),现在分享给大家,希望可以做个参考。
在Lambda的方括号[]中命名局部变量称为捕获变量(capturing the variable)。如果没有在**方括号[]**指定变量,则在Lambda表达中不能使用。Lambda捕获变量默认是值传递方式。
如下面代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41#include <iostream> #include <algorithm> #include <iterator> #include <vector> using namespace std; int main(int argc, char** argv) { // 初始化向量 vector<int> data{1,2,3,4,5,6,7,8,9}; int times = 3; // 输出原始数据 // 输出 cout << "before transform:n"; copy(data.begin(),data.end(),ostream_iterator<int>(cout,",")); cout << endl; // 定义表达 auto op_times = [times](int i){return i * times;}; transform(data.begin(),data.end(),data.begin(),op_times); // 输出 cout << "after transform times=" << times << endl; copy(data.begin(),data.end(),ostream_iterator<int>(cout,",")); cout << endl; // 改变本地变量 times = 5; transform(data.begin(),data.end(),data.begin(),op_times); // 输出 cout << "after transform times=" << times << endl; copy(data.begin(),data.end(),ostream_iterator<int>(cout,",")); cout << endl; return 0; }
程序输出:
当本地变量times的值从3改变为5后,调用transform函数输出的结果不是每个元素乘以5。相当于transform函数调用了两次。为了实现调用结果随着本地变量times的改变而改变,需要通过引用方式捕获变量。
复制代码
1
2auto op_times = [×](int i){return i * times;};
程序运行结果达到预期!
Lambda表达式捕获变量后,被捕获的变量能够在Lambda表达式中改变吗?看看下面的代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <iostream> #include <iostream> #include <algorithm> #include <iterator> #include <vector> using namespace std; int main(int argc, char** argv) { int x{0}; auto test_func = [x](int y){ // 改变参数的值 x = 1; y = 2; return x + y; }; int local{0}; cout <<"after call test_func:"<< test_func(local) <<",x = " << x <<",local=" << local << endl; return 0; }
编译的结果如下:
这说明在值传递方式捕获变量的情况,在Lambda表达式中是不能修改的!
最后
以上就是现实红酒最近收集整理的关于C++11Lambda表达式捕获变量(Capturing the Variable)与本地变量(Local Variable)的全部内容,更多相关C++11Lambda表达式捕获变量(Capturing内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复