免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4013 | 回复: 3
打印 上一主题 下一主题

[C++] 哪位大神能把C++11的lambda说的明白点,透彻点的? [复制链接]

论坛徽章:
5
水瓶座
日期:2013-11-27 23:31:26双鱼座
日期:2014-01-02 15:37:44白羊座
日期:2014-01-07 14:30:09射手座
日期:2014-03-13 17:52:59巨蟹座
日期:2014-04-18 17:43:05
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-12-21 18:40 |只看该作者 |倒序浏览
#include<iostream>#include<vector>#include<functional>#include<string>using namespace std;    class TestThis {public:    TestThis(TestThis &test){cout << "TestThis&" << endl;flag="TestThis&";}    TestThis(TestThis &&test){cout << "TestThis&&" << endl;flag="TestThis&&";}    TestThis(){cout << "TestThis" << endl;flag="TestThis()";}    TestThis(int i):in(i){cout << "TestThis(int)" << i << endl;flag="TestThis(int)";}    ~TestThis(){cout << "~TestThis::"<< flag << this->in << endl;}public:    int in;     int a = 10;     string flag;};int main()    {    TestThis a(7);    cout << "1" << endl;    function<void(TestThis)> f = [a](TestThis b){b.a=100;cout << "func" << endl;};    cout << "2" << endl;    f(a);    cout << "result:" << a.a << endl;    return 0;    }
[color=rgb(38, 38, 3]上面的代码输出:
[color=rgb(38, 38, 3]TestThis(int)7
[color=rgb(38, 38, 3]1
[color=rgb(38, 38, 3]TestThis&
[color=rgb(38, 38, 3]TestThis&&
[color=rgb(38, 38, 3]~TestThis::TestThis&-463406200
[color=rgb(38, 38, 3]2
[color=rgb(38, 38, 3]TestThis&
[color=rgb(38, 38, 3]TestThis&&
[color=rgb(38, 38, 3]TestThis&&
func
~TestThis::TestThis&&2046146688
~TestThis::TestThis&&2046146656
~TestThis::TestThis&0
result:10
~TestThis::TestThis&&0
~TestThis::TestThis(int)7

================================================
将上面的lambda改成
function<void(TestThis&> f = [a](TestThis &b){b.a=100;cout << "func" << endl;};
则输出:
TestThis(int)7
1
TestThis&
TestThis&&
~TestThis::TestThis&0
2
func
result:100
~TestThis::TestThis&&0
~TestThis::TestThis(int)7

=================================================
将上面的代码改成
function<void(TestThis&> f = [&a](TestThis &b){b.a=100;cout << "func" << endl;};
则输出如下:
TestThis(int)7
1
2
func
result:100
~TestThis::TestThis(int)7

论坛徽章:
44
15-16赛季CBA联赛之浙江
日期:2021-10-11 02:03:59程序设计版块每日发帖之星
日期:2016-07-02 06:20:0015-16赛季CBA联赛之新疆
日期:2016-04-25 10:55:452016科比退役纪念章
日期:2016-04-23 00:51:2315-16赛季CBA联赛之山东
日期:2016-04-17 12:00:2815-16赛季CBA联赛之福建
日期:2016-04-12 15:21:2915-16赛季CBA联赛之辽宁
日期:2016-03-24 21:38:2715-16赛季CBA联赛之福建
日期:2016-03-18 12:13:4015-16赛季CBA联赛之佛山
日期:2016-02-05 00:55:2015-16赛季CBA联赛之佛山
日期:2016-02-04 21:11:3615-16赛季CBA联赛之天津
日期:2016-11-02 00:33:1215-16赛季CBA联赛之浙江
日期:2017-01-13 01:31:49
2 [报告]
发表于 2017-12-21 21:01 |只看该作者
本帖最后由 windoze 于 2017-12-21 21:03 编辑

简单说lambda就是个语法糖。

这是lambda

  1. int n=100;
  2. auto l=[&n](){
  3.    n=n+1;
  4. };
  5. cout << n << endl; // 输出100;
  6. l();
  7. cout << n << endl; // 输出101;
  8. l();
  9. cout << n << endl; // 输出102;
复制代码

这是等价的不用lambda的写法:

  1. int n=100;
  2. struct lambda_emulator {
  3.     lambda_emulator(int &v) : n(v) {}
  4.     void operator()() { n=n+1; }
  5.     int &n;
  6. };
  7. lambda_emulator l(n);
  8. cout << n << endl; // 输出100;
  9. l();
  10. cout << n << endl; // 输出101;
  11. l();
  12. cout << n << endl; // 输出102;
复制代码

论坛徽章:
12
2015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之同曦
日期:2017-03-17 19:13:162016科比退役纪念章
日期:2016-11-07 08:28:12luobin
日期:2016-06-17 17:46:36wusuopu
日期:2016-06-17 17:43:4515-16赛季CBA联赛之福建
日期:2016-01-14 12:49:22程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2015-06-08 22:20:00程序设计版块每日发帖之星
日期:2015-06-08 22:20:002015年亚洲杯之科威特
日期:2015-03-24 14:21:272015年迎新春徽章
日期:2015-03-04 09:57:092016科比退役纪念章
日期:2018-04-10 16:20:18
3 [报告]
发表于 2017-12-22 09:02 |只看该作者
这玩意儿挺简单的吧???

论坛徽章:
5
水瓶座
日期:2013-11-27 23:31:26双鱼座
日期:2014-01-02 15:37:44白羊座
日期:2014-01-07 14:30:09射手座
日期:2014-03-13 17:52:59巨蟹座
日期:2014-04-18 17:43:05
4 [报告]
发表于 2017-12-22 16:51 |只看该作者
回复 2# windoze

嗯,开始没明白是怎么实现的。多谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP