zoukankan      html  css  js  c++  java
  • C++11lambda表达式

    C++11lambda表达式

      

      mutable 修饰符,用于修改[]中以值传递的变量,无mutable修饰符的话则不行。

      

     使用示例:

     1 #include <vector>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <functional>
     5  
     6 int main()
     7 {
     8     std::vector<int> c { 1,2,3,4,5,6,7 };
     9     int x = 5;
    10     c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end());
    11  
    12     std::cout << "c: ";
    13     for (auto i: c) {
    14         std::cout << i << ' ';
    15     }
    16     std::cout << '
    ';
    17  
    18     // the type of a closure cannot be named, but can be inferred with auto
    19     auto func1 = [](int i) { return i+4; };
    20     std::cout << "func1: " << func1(6) << '
    '; 
    21  
    22     // like all callable objects, closures can be captured in std::function
    23     // (this may incur unnecessary overhead)
    24     std::function<int(int)> func2 = [](int i) { return i+4; };
    25     std::cout << "func2: " << func2(6) << '
    '; 
    26 }
    View Code

    另一种函数语法

     这种语法也能套用到一般的函数定义与声明:

     参考:

     1、http://www.tuicool.com/articles/MjaaQ3

     

  • 相关阅读:
    从当前url替换获得新的url
    访问者模式
    备忘录模式
    make makefile cmake qmake 区别
    qt编译过程
    tensorflow前处理
    tesorflow操作
    tensorflow的object_detection安装
    tensorflow 编译与训练
    tensorflow后处理
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3500817.html
Copyright © 2011-2022 走看看