一. 匿名函数
简短函数,就地书写,调用。即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;
else
cout<<low(salary)<<endl;
}