C++11重要特性: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]和[=]可以访问这个成员函数所能访问的对象   


理解捕获的概念:

// 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
}

直接调用生成运算结果:

#include <iostream>

int main()
{
   using namespace std;
   int n = [] (int x, int y) { return x + y; }(5, 4);
   cout << n << endl;
}

lambda的嵌套:

#include <iostream>

int main()
{
    using namespace std;

    // The following lambda expression contains a nested lambda
    // expression.
    int timestwoplusthree = [](int x) { return    [](int y) { return y * 2; }(x) + 3; }(5);

    // Print the result.
    cout << timestwoplusthree << endl;
}

更高级嵌套的:
#include <iostream>
#include <functional>
int main()
{
    using namespace std;
    auto addtwointegers = [](int x) -> function<int(int)> { 
        return [=](int y) { return x + y; }; 
    };

    auto higherorder = [](const function<int(int)>& f, int z) { 
        return f(z) * 2; 
    };
    auto answer = higherorder(addtwointegers(7), 8);
    cout << answer << endl;
}

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;
}

当然最好是能够学习function的原理,参考博客:http://blog.163.com/lvan100@yeah/blog/static/6811721420127511289750/



3.


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值