Ai
1 Star 0 Fork 0

Hu_YuChen/ Code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lambda.cpp 623 Bytes
一键复制 编辑 原始数据 按行查看 历史
Hu_YuChen 提交于 2025-04-29 20:16 +08:00 . C++
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void processVector(const vector<int>&vec,void(*func)(int))
{
for(int x : vec)
{
func(x);
}
}
int main()
{
int factor = 2;
int a = 2, b = 3;
vector<int> vec = { 1,2,3,4,5,6 };
//捕获变量
for_each(vec.begin(), vec.end(), [&factor](int x)
{
cout << x * factor << endl;
factor++;
});
cout << endl;
cout << "Final value of factor:" << factor << endl;
//捕获所有变量
auto lambda = [=]() {return a * b; };
cout << "Result:"<<lambda()<<endl;
//lambda作为参数
processVector(vec, [](int x) {cout << x * x << " "; });
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hu_yuchen/code.git
git@gitee.com:hu_yuchen/code.git
hu_yuchen
code
Code
master

搜索帮助