Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2571668
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: C/C++

2013-05-05 19:06:00

该出手时就出手...


点击(此处)折叠或打开

  1. // Lambda.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include <iostream>            // std::cout std::fixed
  5. #include <iomanip>            // std::setprecision
  6. //#include <ctime>            // time
  7. //#include <cstdlib>
  8. #include <map>                // std::map
  9. #include <string>            // std::string
  10. #include <algorithm>        // std::find_if, std::for_each
  11. #include <vector>            // std::vector
  12. //#include <list>            // std::list

  13. using namespace std;

  14. #define SHOW_VECTOR(T, vct)    \
  15.     for (vector<T>::iterator it = vct.begin(); it != vct.end(); )    \
  16. {    \
  17.     cout << *(it++) << endl;\
  18. }\

  19. int _tmain(int argc, _TCHAR* argv[])
  20. {
  21.     ///< Lambda表达式三种方式之[&] - 传引用的方式使用【当前作用域】
  22.     vector<int> vint;
  23.     vint.push_back(4);
  24.     vint.push_back(5);
  25.     int vintTotal = 0;
  26.     for_each(vint.begin(), vint.end(),
  27.              [&](int vint_element)
  28.              {
  29.                  vintTotal += vint_element;
  30.              });
  31.     cout << "获得容器总和: " << vintTotal << endl;
  32.     //说明:这里我们需要改变当前作用域变量的值,也就是vintCounts,因此用[&]说明Lambda表达式以传引用的方式使用当前作用域内的变量;




  33.     ///< Lambda表达式三种方式之[=] - 传值的方式使用 【当前作用域】
  34.     map<string, char> mapstring;
  35.     char grade = 'C';
  36.     mapstring.insert(pair<string, char>("李海锦", 'A'));
  37.     mapstring.insert(pair<string, char>("黄磊", 'B'));
  38.     map<string, char>::iterator itmap = mapstring.begin();
  39.     for_each(mapstring.begin(), mapstring.end(),
  40.         [=](pair<string, char> itermap)
  41.     {
  42.         //itermap.second = 'C'; // 无效
  43.         itmap->second = grade;    //只读方式获取当前作用域变量grade的值
  44.         //itmap++; //这里不能++了,因为[=]传递方式是值传递,为了测试
  45.     });
  46.     // show content:
  47.     for (std::map<string, char>::iterator it = mapstring.begin();
  48.         it != mapstring.end(); ++it)
  49.     {
  50.         cout << it->first << " => " << it->second << '\n';
  51.     }
  52.     //说明: 传值方式就是不想改变当前作用域变量的值,而是以只读方式获取变量值加以利用




  53.     ///< Lambda表达式三种方式之[&, =, var ...] 多种形式
  54.     vector<double> vdouble;
  55.     int vdoubleId = 0;
  56.     vdouble.push_back(4000.0);
  57.     vdouble.push_back(5003.0);
  58.     double magnification = 0.15;    ///< 哇,程序员工资降得太低了...
  59.     for_each(vdouble.begin(), vdouble.end(),
  60.         [&, magnification](double vdouble_element)
  61.     {
  62.         //cout << setprecision(6) << vdouble_element << endl;
  63.         vdouble[vdoubleId++] = vdouble_element * magnification;
  64.         /*magnification += 1.8;*/    ///< not OK,当前是值传递方式
  65.     });
  66.     SHOW_VECTOR(double, vdouble);
  67.     //说明: 混合方式,默认是传引用方式:当前vdoubleId需要传引用方式,而magnification则只需要传值方式,因此[]第二个参数写变量名,表示传值方式;





  68.     ///< Lambda表达式三种方式之返回值,借用书中一列
  69.     int nEven = count_if(
  70.             vdouble.begin(), vdouble.end(),
  71.             [=](double vdouble) -> bool
  72.             {
  73.                 return vdouble < 4000;
  74.             }
  75.         );
  76.     cout << "满足条件的个数 " << nEven << endl;
  77.     //说明: 在某些STL算法中可能需要某些函数有返回值,则可以如此;





  78.     ///< Lambda表达式三种方式之封装为函数,进行复用
  79.     auto modifySalary = [&, magnification](double vdouble_element)
  80.     {
  81.         //cout << setprecision(6) << vdouble_element << endl;
  82.         vdouble[vdoubleId++] = vdouble_element * magnification;
  83.         /*magnification += 1.8;*/    ///< not OK,当前是值传递方式
  84.     };
  85.     //说明: 以上如果需要都可以封装为函数进行复用

  86.     getchar();
  87.     return 0;
  88. }

阅读(898) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~