C++新特性探究(十):Lambda
一. 匿名函数
简短函数,就地书写,调用。即Lambda存在的意义,常用于取代作回调用的简短函数指针与仿函数。
就地书写,因只有函数体,即无函数名,也称匿名函数。
格式
最小的 Lambda:[ ] { }
例1:
附例1代码:
//小问学编程
#include<iostream>using namespace std;int main()
{auto f=[]{return 1+2;};cout<<f()<<endl;cout<<[]{return 1+2;}()<<endl;return 0;
}
第二小的 Lambda:[ ] ( ) { }
例2:
附例2代码:
//小问学编程
#include<iostream>using namespace std;int main()
{auto f=[](int x,int y){return x+y;};cout<<f(5,6)<<endl;cout<<[](int x,int y){return x+y;}(3,4)<<endl;return 0;
}
例3
附例3代码:
//小问学编程
#include<iostream>using namespace std;int main()
{auto f=[](int x=10,int y=20){return x+y;};cout<<f(5,6)<<endl;cout<<[](int x=10,int y=20){return x+y;}()<<endl;return 0;
}
小的 Lambda:[ ] ( ) -> { }
例4
附例4代码:
//小问学编程
#include<iostream>using namespace std;int main()
{auto f=[](int x=10,int y=20)->int{return x+y;};cout<<f(5,6)<<endl;cout<<[](int x=10,int y=20)->int{return x+y;}()<<endl;return 0;
}
三. 闭包[ ]与mutable
闭包
closure是一个函数和它所引用的非本地变量的上下文环境的集合。从定义我们可以得知,closure可以访问在它定义范围之外的变量,也即non-local vriables(非局部变量)。关于closure的最重要的应用就是回调函数。
Lambda函数能够捕获Lambda函数外的具有自动存储时期的变量。函数体与这些变量的集合合起来叫闭包。
闭包的概念在Lambda中谈过[ ]来体现出来。
例5:
附例5代码:
//小问学编程
#include<iostream>
using namespace std;int main()
{int x=100,y=200;auto f=[&](){x=50;y=50;cout<<x<<":"<<y<<endl;};f();cout<<x<<":"<<y<<endl;return 0;
}
例6:[ ]mutable
附例6代码:
//小问学编程
#include<iostream>using namespace std;int main()
{int x=100,y=200;auto f=[=]()mutable{x=50;y=50;cout<<x<<":"<<y<<endl;};f();cout<<x<<":"<<y<<endl;return 0;
}
例7:
附例7代码:
//小问学编程
#include<iostream>
using namespace std;int main()
{int x=100,y=200;auto f=[x]()mutable{int sum=10;sum+=x;x+=20;cout<<x<<":"<<sum<<endl;};f();cout<<x<<":"<<y<<endl;return 0;
}
例8:
附例8代码:
//小问学编程
#include<iostream>
using namespace std;int main()
{int x=100,y=200;auto f=[&x]()mutable{int sum=10;sum+=x;x+=20;cout<<x<<":"<<sum<<endl;};f();cout<<x<<":"<<y<<endl;return 0;
}
例9:
附例9代码:
//小问学编程
#include<iostream>
using namespace std;int main()
{int x=100,y=200;auto f=[&x,y]()mutable{int sum=10;sum+=x;x+=20;y+=20;cout<<x<<":"<<y<<":"<<sum<<endl;};f();cout<<x<<":"<<y<<endl;return 0;
}
捕获规则:
闭包及Lambda本质
Lambda即匿名对象,闭包的本质,初始化Lambda对象。
四. 闭包的应用
1. STL::for_each
例10:
附例10代码:
//小问学编程
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;int main()
{vector<int> vi={1,2,3,4,5,6,7};for_each(vi.begin(),vi.end(),[](int& i){cout<<i<<" ";});return 0;
}
2. QT中signal和slot机制
代码:
六. Lambda与仿函数
Lambda本质就是匿名的仿函数
回顾:C++新特性探究(九):functor仿函数
例11:
附例11代码:
//小问学编程
#include <iostream>
using namespace std;int main()
{double highrate = 0.3;double highbase = 30000;auto high = [highrate,highbase](double money){return (money-highbase)*highrate;};double midrate = 0.3;double midbase = 30000;auto middle = [midrate,midbase](double money){return (money-midbase)*midrate;};double lowrate = 0.1;double lowbase = 3500;auto low = [lowrate,lowbase](double money){return (money-lowbase)*lowrate;};cout<<"请输入你的收入:";double salary;cin>>salary;if(salary>30000)cout<<high(salary)<<endl;else if(salary >15000)cout<<middle(salary)<<endl;elsecout<<low(salary)<<endl;
}
C++新特性探究(十):Lambda相关推荐
- C++新特性探究(十四):function
相关博文:C++头文件<functional>和bind.placeholders占位符使用简单例子 相关博文:<Essential C++>笔记之设计一个泛型算法(二) 相关 ...
- C++新特性探究(十六):move constructor移动构造
相关博文: C++新特性探究(十三):右值引用(r-value ref)&&探究 C++新特性探究(十六):move constructor移动构造 C++新特性探究(13.5):右值 ...
- C++新特性探究(十五):bind
相关博文:C++新特性探究(十四):function 相关博文:C++头文件<functional>和bind.placeholders占位符使用简单例子 相关博文:<Essenti ...
- C++新特性探究(十八):智能指针
一.智能指针及RAII 问题: C++中最令人头疼的问题是强迫程序员对申请的资源(文件,内存等)进行管理,一不小心就会出现泄露(忘记对申请的资源进行释放)的问题. C++的解决办法:RAII ...
- C++新特性探究(十二):static_assert(提前判误)
相关博文:C++之异常处理探究 相关博文:assert.if else.try catch三者的区别 相关博文:C++之assert.NDEBUG探究 相关博文:static_assert和asser ...
- C++新特性探究(13.6):右值引用再探究
相关博文: C++新特性探究(十三):右值引用(r-value ref)&&探究 C++新特性探究(十六):move constructor移动构造 C++新特性探究(13.5):右值 ...
- C++新特性探究(13.5):右值引用
相关博文: C++新特性探究(十三):右值引用(r-value ref)&&探究 C++新特性探究(十六):move constructor移动构造 C++新特性探究(13.5):右值 ...
- C++新特性探究(十三):右值引用(r-value ref)探究
相关博文: C++新特性探究(十三):右值引用(r-value ref)&&探究 C++新特性探究(十六):move constructor移动构造 C++新特性探究(13.5):右值 ...
- Java 8新特性探究(二)深入解析默认方法
转载自 Java 8新特性探究(二)深入解析默认方法 什么是默认方法,为什么要有默认方法 简单说,就是接口可以有实现方法,而且不需要实现类去实现其方法.只需在方法名前面加个default关键字即可. ...
最新文章
- Centos7 安装 nginx 服务器的两种方式
- simple-android-flux,深入浅出Flux
- Transformer升级之路:博采众长的旋转式位置编码
- 2021技术领域趋势报告:Rust继续增长、低代码是重要趋势
- java接口文档生成工具_接口文档生成
- python3 tkinter详解_python tkinter基本属性详解
- 通信要学很多计算机课吗,辽宁科技学院通信工程专业要学哪些课程,好学吗?...
- 电商场景中的精排服务实践
- V神演讲干货全送上!关于以太坊2.0,你想知道的都在这里!
- 官方demo修改后的webuploader上传预览图片(兼容IE8) github下载回来的有问题
- 华三ap设置无线服务器,H3C无线控制器实现Remote AP功能典型配置举例(V7)
- SVG 动画(animate、animateTransform、animateMotion)
- HeadFirstJava 7,8,9
- win10激活bug 任务栏假死点击无反应解决方案
- 深入浅出Word2Vec原理解析
- 串口热拔插学习(转载)
- 门这边、门那边的2个世界...
- (保姆级)利用ffmpeg将flv批量转mp4
- mysql中checktable语句来_MySQL的命令check table用法
- 论文解读:《DeepIDC:基于异构信息和深度学习的注射用药物组合预测框架》