C++0x-Lambda表达式

发布时间:2020-05-03 23:00:40 作者:mengxm826
来源:网络 阅读:697

 

  1. /* 
  2.  * 注意:Lambdas表达式隐式定义并构建不具名函数对象 
  3.  */ 
  4.   
  5. /////////////////////////////////////////////////////////////////////////////////////////////////////////  
  6.  
  7. /* 
  8.  * []操作符为lambda引导符,(int n)为lambda参数声明 
  9.  * 不具名函数对象类的函数调用操作符默认返回void 
  10.  * {}说明函数体是复合语句 
  11.  * 由以下代码可以看出lambda表达式的效果和函数对象一样 
  12.  */ 
  13.   
  14. // 实例代码1:  
  15. #include <algorithm> 
  16. #include <iostream> 
  17. #include <vector> 
  18. using namespace std; 
  19.  
  20. struct LambdaFunctor { 
  21.     void operator() (int n) { 
  22.         cout << n << " "
  23.     } 
  24. }; 
  25.  
  26. int main() { 
  27.     vector <int> v; 
  28.  
  29.     for (int i = 0; i < 10; ++i) { 
  30.         v.push_back(i); 
  31.     } 
  32.  
  33.     for_each (v.begin(), v.end(), [] (int n) { 
  34.         cout << n << " ";    
  35.     }); 
  36.     cout << endl; 
  37.  
  38.     for_each (v.begin(), v.end(), LambdaFunctor()); 
  39.     cout << endl; 
  40.  
  41.     return 0; 
  42. ///////////////////////////////////////////////////////////////////////////////////////////////////////// 
  43.  
  44. /* 
  45.  * lambda表达式中若有{return expression;}  
  46.  * 则lambda的返回类型就会自动被推断成expression的类型 
  47.  * 但是过于复杂的声明语句lambda函数不会自动推断返回类型 
  48.  * 必须显示指定返回类型(实质是函数体中只有一个返回语句时可推断) 
  49.  *  
  50.  * 注意:-> type 指定返回值类型,必须置于()参数表后 
  51.  */ 
  52.   
  53. // 实例代码2: 
  54. #include <algorithm> 
  55. #include <iostream> 
  56. #include <vector> 
  57. using namespace std; 
  58.  
  59. template <typename func> 
  60. void test(func a) { 
  61.     if (a(1, 0)) { 
  62.         cout << 1 << endl; 
  63.     } else { 
  64.         cout << 0 << endl; 
  65.     } 
  66.  
  67. int main() { 
  68.     test([] (int a, int b) { 
  69.         return a > b; 
  70.     }); 
  71.     return 0; 
  72. }  
  73.  
  74. // 实例代码3: 
  75. #include <algorithm> 
  76. #include <iostream> 
  77. #include <vector> 
  78. using namespace std; 
  79.  
  80. template <typename func> 
  81. void test(func a) { 
  82.     cout << a(1, 0) << endl; 
  83.  
  84. int main() { 
  85.     test([] (int a, int b) -> int { 
  86.         if (a % 2 == 0) { 
  87.             return a * a * a; 
  88.         } else { 
  89.             return a / 2; 
  90.         } 
  91.     }); 
  92.     return 0; 
  93. ///////////////////////////////////////////////////////////////////////////////////////////////////////// 
  94.  
  95. /* 
  96.  * 通常空[]表示lambda是无状态的,即不包含数据成员 
  97.  * 在lambda引导符[]中可以指定capture-list 
  98.  * 函数对象和lambda效果一样,函数对象存储了局部变量的拷贝 
  99.  * 所以本质上“按值”传递对lambda而言就是隐式创建韩局部变量函数对象 
  100.  * 注意点: 
  101.  * (a)在lambda中不能修改通过传递获得的拷贝,默认情况下函数调用操作符是const属性 
  102.  * (b)有些对象拷贝开销大 
  103.  * (c)局部变量的更新不会反应到通过传递获得的拷贝  
  104.  */ 
  105.   
  106. // 实例代码4: 
  107. #include <algorithm> 
  108. #include <iostream> 
  109. #include <vector> 
  110. using namespace std; 
  111.  
  112. // 创建含局部变量的函数对象 
  113. struct LambdaFunctor { 
  114.     LambdaFunctor(int a, int b) : m_a(a), m_b(b) {} 
  115.  
  116.     bool operator() () const { 
  117.         if (m_a % 2 == 0) { 
  118.             return m_a * m_a * m_a; 
  119.         } else { 
  120.             return m_a / 2; 
  121.         } 
  122.     } 
  123.  
  124. private
  125.     int m_a, m_b; 
  126. }; 
  127.  
  128. template <typename func> 
  129. void test(func a) { 
  130.     cout << a() << endl; 
  131.  
  132. int main() { 
  133.     int a = 1, b = 0; 
  134.  
  135.     test([a, b] () -> int { 
  136.         if (a % 2 == 0) { 
  137.             return a * a * a; 
  138.         } else { 
  139.             return a / 2; 
  140.         } 
  141.     }); 
  142.  
  143.     test(LambdaFunctor(a, b)); 
  144.     return 0; 
  145. ///////////////////////////////////////////////////////////////////////////////////////////////////////// 
  146.  
  147. /* 
  148.  * 也可以利用[=]形式lambda引导符“按值传递任何东西” 
  149.  * 利用mutable将函数调用操作符转为non-const属性 
  150.  */ 
  151.  
  152. // 实例代码5: 
  153. #include <algorithm> 
  154. #include <iostream> 
  155. #include <vector> 
  156. using namespace std; 
  157.  
  158. template <typename func> 
  159. void test(func a) { 
  160.     cout << a() << endl; 
  161.  
  162. int main() { 
  163.     int a = 1, b = 0; 
  164.  
  165.     test([=] () mutable -> int { 
  166.         if (a % 2 == 0) { 
  167.             a = 2; 
  168.             return a * a * a; 
  169.         } else { 
  170.             a = 4; 
  171.             return a / 2; 
  172.         } 
  173.     }); 
  174.  
  175.     return 0; 
  176. ///////////////////////////////////////////////////////////////////////////////////////////////////////// 
  177.  
  178. /* 
  179.  * 为了避免拷贝的开销出现通过引用传递 
  180.  * 语法形式[&x, &y],隐式构建局部变量为引用变量 
  181.  * 也可以用[&]来表示“按引用传递任何东西” 
  182.  * 可以不用mutable改变属性 
  183.  * 可以在引导符[]进行混合使用传递,例如:[&,x,y],[x,&a,y,&b] 
  184.  */ 
  185.   
  186. // 实例代码6: 
  187. #include <algorithm> 
  188. #include <iostream> 
  189. #include <vector> 
  190. using namespace std; 
  191.  
  192. template <typename func> 
  193. void test(func a) { 
  194.     cout << a() << endl; 
  195.  
  196. int main() { 
  197.     int a = 1; 
  198.  
  199.     test([&a] () -> int { 
  200.         if (a % 2 == 0) { 
  201.             a = 2; 
  202.             return a * a * a; 
  203.         } else { 
  204.             a = 4; 
  205.             return a / 2; 
  206.         } 
  207.     }); 
  208.  
  209.     cout << a << endl; 
  210.     return 0; 
  211. ///////////////////////////////////////////////////////////////////////////////////////////////////////// 
  212.  
  213. /* 
  214.  * 注意有一种特别传参用法this 
  215.  * 但无法获得一个lambda对象他自身的this指针 
  216.  * [=],[&]可以隐式传递this,[&this]不可以使用 
  217.  */ 
  218.  
  219. ///////////////////////////////////////////////////////////////////////////////////////////////////////// 
  220.  
  221. /* 
  222.  * 当lambda不带参数时可以不加()但是同样也不能显示指定返回值类型 
  223.  * 但是可以隐式推断返回值类型 
  224.  * 定义lambda表达式后可以立即用()操作符使用表达式 
  225.  * 可以用#define来简化定义一个lambda表达式 
  226.  * 但是不可以用typedef来定义一个类型 
  227.  */  
  228.   
  229. // 实例代码7: 
  230. #include <algorithm> 
  231. #include <iostream> 
  232. #include <vector> 
  233. using namespace std; 
  234.  
  235. template <typename func> 
  236. void test(func a) { 
  237.     cout << a() << endl; 
  238.  
  239. int main() { 
  240.     int a = 1; 
  241.  
  242.     test([] { 
  243.         return 9; 
  244.     }); 
  245.  
  246.     return 0; 
  247.  
  248. // 实例代码8: 
  249. #include <algorithm> 
  250. #include <iostream> 
  251. #include <vector> 
  252. using namespace std; 
  253.  
  254. template <typename func> 
  255. void test(func a) { 
  256.     cout << a() << endl; 
  257.  
  258. #define mengxm [] () { cout << "mengxm" << endl; } 
  259.  
  260. int main() { 
  261.  
  262.     mengxm(); 
  263.      
  264.     return 0; 

 

推荐阅读:
  1. shell的条件表达式、文件测试表达式、逻辑测试表达式等等?
  2. 表达式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++0x-lambda表达式 c+ %d

上一篇:Ubuntu下切换多个JDK环境

下一篇:那些年干过的事(六)—无源码修改版本号

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》