联系客服:400-805-1963

C++0x-Lambda表达式
更新:HHH   时间:2023-1-7


 

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

 

返回移动开发教程...