C++ []lambda表达式
01_lambda表达式.cpp
#include<iostream>
#include<algorithm>
using namespace std;
class Func_call{
public:
// Func_call(){cout<<"Construction"<<endl;}
// ~Func_call(){cout<<"Destructor"<<endl;}
//重载了函数调用运算符的类,在其调用运算符执行时,可以称作函数对象
bool operator()(int a , int b)
{
cout<<"operator()"<<endl;
return a<b;
}
};
int main()
{
int i =100;
int m[]={34,45,312,3,21,4,3246};
for(auto k:m)
cout<<k<<" ";
cout<<endl;
// sort(m,m+sizeof(m)/sizeof(m[0]),
// Func_call()
// );
sort(m,m+sizeof(m)/sizeof(m[0]),
//lambda 兰布达表达式是一种匿名函数的定义方式
//但是其捕获器是专有的
//[]是捕获器
//(int a,int b)参数
//->bool 尾置返回类型
//{} 执行体
[&i](int a,int b)->bool
{
cout<<"i="<<i++<<endl;
return a>b;
}
);
cout<<"sorted:"<<endl;
for(auto k:m)
cout<<k<<" ";
cout<<endl;
cout<<"main i="<<i<<endl;
}