lambda+function

1. lambda

  1. lambda-introducer (捕获字段)
  2. lambda-parameter-declaration-list (变量列表)
  3. mutable-specification (捕获的变量可否修改)
  4. exception-specification (异常设定)
  5. lambda-return-type-clause (返回类型)
  6. compound-statement (函数体)
捕获字段:空,=,&

空:不能访问外部变量

=:按值访问外部变量,[var]按值访问var,[=]按值访问所有变量

&:引用方式访问外部变量,[&var]引用访问var变量,[&]引用访问所有变量

组合[=,&var]能够按照引用访问var和按值访问所有变量

特殊情况:lambda函数在某个成员函数里面时,[this]和[=]可以访问这个成员函数所能访问的对象   

lambda表达式的作用是可以替换函数指针。
// 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
}
直接调用生成运算结果:

[cpp]  view plain  copy
  1. #include <iostream>  
  2.   
  3. int main()  
  4. {  
  5.    using namespace std;  
  6.    int n = [] (int x, int y) { return x + y; }(5, 4);  
  7.    cout << n << endl;  
  8. }  

lambda的嵌套:

[cpp]  view plain  copy
  1. #include <iostream>  
  2.   
  3. int main()  
  4. {  
  5.     using namespace std;  
  6.   
  7.     // The following lambda expression contains a nested lambda  
  8.     // expression.  
  9.     int timestwoplusthree = [](int x) { return    [](int y) { return y * 2; }(x) + 3; }(5);  
  10.   
  11.     // Print the result.  
  12.     cout << timestwoplusthree << endl;  
  13. }  

更高级嵌套的:
[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <functional>  
  3. int main()  
  4. {  
  5.     using namespace std;  
  6.     auto addtwointegers = [](int x) -> function<int(int)> {   
  7.         return [=](int y) { return x + y; };   
  8.     };  
  9.   
  10.     auto higherorder = [](const function<int(int)>& f, int z) {   
  11.         return f(z) * 2;   
  12.     };  
  13.     auto answer = higherorder(addtwointegers(7), 8);  
  14.     cout << answer << endl;  
  15. }  

addtwointegers(7)生成了一个function<int(int)>函数,函数功能是计算7+输入的累加和

higherorder 可以看做是一个更高级的lambda,调用生成的addtwointegers(7),给它传入一个参数,计算结果

2. function包装器

function<>包装器可以将普通函数,lambad函数,函数对象统一分装起来,虽然它们不是相同的类型,但是经过了function模板后,它们可以转化为相同的function的对象

将函数作为参数,解决模板匹配效率低的问题。

int add(int i, int j) { return i + j; }  //普通函数
// lambda表达式
auto mod = [](int i, int j){return i % j; };
// 函数对象类
struct divide
{
	int operator() (int denominator, int divisor)
	{
		return denominator / divisor;
	}
};

int main(int argc, char *argv[])
{
	map<char, function<int(int, int)>> binops = 
	{
		{ '+', add },
		{ '-', minus<int>() },
		{ '*', [](int i, int j){return i * j; } },
		{ '/', divide() },
		{ '%', mod },
	};
//函数有两个参数,
	cout << binops['+'](10, 5) << endl;
	cout << binops['-'](10, 5) << endl;
	cout << binops['*'](10, 5) << endl;
	cout << binops['/'](10, 5) << endl;
	cout << binops['%'](10, 5) << endl;
	system("pause");
	return 0;
}















猜你喜欢

转载自blog.csdn.net/runner668/article/details/80159184