捕获字段:空,=,&
空:不能访问外部变量
=:按值访问外部变量,[var]按值访问var,[=]按值访问所有变量
&:引用方式访问外部变量,[&var]引用访问var变量,[&]引用访问所有变量
组合[=,&var]能够按照引用访问var和按值访问所有变量
特殊情况:lambda函数在某个成员函数里面时,[this]和[=]可以访问这个成员函数所能访问的对象
理解捕获的概念:
// declaring_lambda_expressions2.cpp
// compile with: /EHsc /W4
#include <functional>
#include <iostream>
int main()
{
using namespace std;
int i = 3;
int j = 5;
// The following lambda expression captures i by value and
// j by reference.
function<int (void)> f = [i, &j] { return i + j; };//声明定义f的时候,使用i的值传入,j的引用
// Change the values of i and j.
i = 22;
j = 44;
// Call f and print its result.
cout << f() << endl; //output:3+44=47
}
1、获取变量类型
1. #include <typeinfo.h>
2.
3. int a = 1;
4.
5. typeid(a).name();
2、lambda表达式用法
C++11 提供了对匿名函数的支持,称为 Lambda 函数(也叫 Lambda 表达式)。Lambda 表达式把函数看作对象。Lambda 表达式可以像对象一样使用,比如可以将它们赋给变量和作为参数传递,还可以像函数一样对其求值。Lambda 表达式本质上与函数声明非常类似。
1. [capture](parameters)->return-type{body}
2.
3. //例如
4. [](int x, int y) -> int { int z = x + y; return z + x; }
5.
6. int main(){
7. int a = 1;
8. int b = 2;
9.
10. return a + b;};
11. //error! 空捕获列表,无法使用作用域内其他变量
12.
13. int a, int b){return a + b;};
14. //success
15.
16. return a + b;};
17. //success, 值传递
18.
19. return a + b;};
20. //error! 值传递无法修改变量值
21.
22. return a + b;};
23. //success, 引用传递
24.
25. return a + b;};
26. //error, 变量a引用传递,变量b值传递,故b不可修改
27.
28. }
3、将lambda表达式作为函数参数传递
想要使lambda作为参数,首先要明白他的类型,但是auto无法作为函数参数类型,使用1中获取类型方法输出为【Z4mainEUlvE_23】,根本不是类型,查阅资料,只能使用template模板来实现。
1. template<typename T>
2. struct isFunctor : std::false_type {
3. };
4. template<typename L, typename R, typename... Args>
5. struct isFunctor<R (L::*)(Args...)> : std::true_type {
6. };
7. template<typename L>
8. struct isLambda : isFunctor<decltype(&L::operator())> {
9. };
10.
11. template<typename L>
12. std::enable_if<isLambda<L>::value> check(L lambda) {
13. " lambda(1, 2) = " << lambda(1, 2) << lambda(1, 2);
14. }
15. int main() {
16. int a, int b) {
17. a++;
18. b++;
19. "."<< b << endl;
20. return pow(a, b);
21. };
22. check(fun);
23. return 0;
24. }
25.
26.
27. //输出为: 2.3
28. // 2.3
29. // lambda(1, 2) = 88
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
//lambda基础
void lambda_one();
//sort中lambda实例
void lambda_two();
//lambda函数递归
void lambda_three();
int main()
{
lambda_one();
lambda_two();
lambda_three();
getchar();
return 0;
}
void lambda_one()
{
cout<<endl <<"------------没有函数参数的lambda---------------------"<<endl;
auto fun = [](){};
auto fun1 = [](){cout <<"fun1"<<endl;};
fun1();
cout<<endl <<"------------for_each中使用简单的lambda----------------"<<endl;
std::vector<int> v(3,5);
for_each(v.begin(),v.end(),[](int num){ cout << num << "\t";});
cout<<endl <<"------------设置lambda的返回值类型--------------------"<<endl;
cout <<[](double a, double b){return a + b;}(1.4,2.5)<<endl;
cout <<[](double a, double b)->int{return a + b;}(1.4,2.5)<<endl;
cout<<endl <<"------------lambda中的传值----------------------------"<<endl;
int x = 10;
int y = 100;
[=](double a, double b)mutable->int
{
cout <<"lambda:"<<(x = 100) << "\t" <<(y = 10)<<endl;
return a + b;
}(1.4,2.5);
cout<<"main:" << x <<"\t"<< y <<endl;
cout<<endl <<"------------lambda中的传引用--------------------------"<<endl;
[&x,&y](double a, double b)mutable->int
{
cout <<"lambda:"<<(x = 100) << "\t" <<(y = 10)<<endl;
return a + b;
}(1.4,2.5);
cout<<"main:" << x <<"\t"<< y <<endl;
cout<<endl <<"------------lambda中的传引用和引用传递------------------"<<endl;
//等号必须写在前面,或者也可以[x,&y].
//=表示,除了&y,其他所有的外部变量都可以按照值传递进行访问。
[=,&y](double a, double b)mutable->int
{
cout <<"lambda:"<<(x = 100) << "\t" <<(y = 10)<<endl;
return a + b;
}(1.4,2.5);
cout<<"main:" << x <<"\t"<< y <<endl;
}
void lambda_two()
{
//1.sort排序
cout <<endl<<"------------sort排序中使用lambda------------------------"<<endl;
int a[8] = {6,8,3,4,9,2,7,1};
sort(begin(a),end(a),[](const int &a ,const int &b)->bool{return a < b;});
for_each(begin(a),end(a),[](const int &num){cout << num << "\t";});
cout <<endl<<"---------------------------------------------------------"<<endl;
}
void lambda_three()
{
//2. (lambda递归)3个数返回最大的两个数的和
cout <<endl<<"------------lambda递归-----------------------------------"<<endl;
function<int(int,int,int)> f = [&f](int a,int b,int c)->int
{
if (a<= b && a <= c)
{
return b + c;
}
return f(b,c,a);
};
cout << f(4,5,6)<<endl;
}