~~~~我的生活,我的点点滴滴!!
C++ Lambda表达式基本用法
创建一个匿名函数并执行。Objective-C采用的是上尖号^,而C++ 11采用的是配对的方括号[]。实例如下:
#include <iostream>
using namespace std;
int main()
{
[]{
cout << "Hello,World\n";
}();
}
我们也可以方便的将这个创建的匿名函数赋值出来调用:
#include <iostream>
using namespace std;
int main()
{
int i = 1024;
auto func = [](int i) { // (int i) 是指传入改匿名函数的参数
cout << i;
};
func(i);
}
捕获选项
- [] Capture nothing (or, a scorched earth strategy?)
- [&] Capture any referenced variable by reference
- [=] Capture any refe

本文介绍了C++ Lambda表达式的基本用法和不同捕获选项,包括不捕获任何变量、拷贝捕获、引用捕获、混合捕获以及通过`this`捕获对象的指针。详细讲解了每种捕获方式的使用和示例,强调了如何正确捕获并使用外部变量。
最低0.47元/天 解锁文章
1867

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



