C++11中的lambda表达式是什么?我什么时候用?他们解决了哪类在介绍之前不可能解决的问题?

一些示例和用例将是有用的。


当前回答

问题是

C++包括一些有用的通用函数,如std::for_each和std::transform,它们非常方便。不幸的是,它们使用起来也很麻烦,特别是如果要应用的函子对于特定函数是唯一的。

#include <algorithm>
#include <vector>

namespace {
  struct f {
    void operator()(int) {
      // do something
    }
  };
}

void func(std::vector<int>& v) {
  f f;
  std::for_each(v.begin(), v.end(), f);
}

如果只在特定的地方使用f一次,那么编写一个完整的类来做一些琐碎的事情似乎是过度的。

在C++03中,您可能会尝试编写以下内容,以保持函子为本地函数:

void func2(std::vector<int>& v) {
  struct {
    void operator()(int) {
       // do something
    }
  } f;
  std::for_each(v.begin(), v.end(), f);
}

但是这是不允许的,f不能传递给C++03中的模板函数。

新的解决方案

C++11引入了lambdas,它允许您编写一个内联的匿名函子来替换结构f。对于小的简单示例,这样读起来更干净(它将所有内容都放在一个地方),维护起来也可能更简单,例如以最简单的形式:

void func3(std::vector<int>& v) {
  std::for_each(v.begin(), v.end(), [](int) { /* do something here*/ });
}

Lambda函数只是匿名函子的语法糖。

返回类型

在简单的情况下,lambda的返回类型是为您推导的,例如:

void func4(std::vector<double>& v) {
  std::transform(v.begin(), v.end(), v.begin(),
                 [](double d) { return d < 0.00001 ? 0 : d; }
                 );
}

然而,当您开始编写更复杂的lambda时,您将很快遇到编译器无法推断返回类型的情况,例如:

void func4(std::vector<double>& v) {
    std::transform(v.begin(), v.end(), v.begin(),
        [](double d) {
            if (d < 0.0001) {
                return 0;
            } else {
                return d;
            }
        });
}

要解决此问题,您可以使用->T显式指定lambda函数的返回类型:

void func4(std::vector<double>& v) {
    std::transform(v.begin(), v.end(), v.begin(),
        [](double d) -> double {
            if (d < 0.0001) {
                return 0;
            } else {
                return d;
            }
        });
}

“捕获”变量

到目前为止,除了在lambda中传递给lambda之外,我们还没有使用其他变量,但我们也可以在lambda内使用其他变量。如果您想访问其他变量,可以使用capture子句(表达式的[]),该子句在这些示例中尚未使用,例如:

void func5(std::vector<double>& v, const double& epsilon) {
    std::transform(v.begin(), v.end(), v.begin(),
        [epsilon](double d) -> double {
            if (d < epsilon) {
                return 0;
            } else {
                return d;
            }
        });
}

您可以通过引用和值捕获,可以分别使用&和=指定:

[&epsilon,zeta]通过引用获取epsilon并通过值获取zeta[&]通过引用捕获lambda中使用的所有变量[=]按值捕获lambda中使用的所有变量[&,epsilon]通过引用捕获lambda中使用的所有变量,但通过值捕获epsilon[=,&epsilon]通过值捕获lambda中使用的所有变量,但通过引用捕获epsilon

默认情况下,生成的运算符()是常量,这意味着当您默认访问捕获时,捕获将是常量。这样做的效果是,使用相同输入的每个调用都会产生相同的结果,但是您可以将lambda标记为可变,以请求生成的运算符()不是常量。

其他回答

Lambda表达式通常用于封装算法,以便将它们传递给另一个函数。但是,可以在定义后立即执行lambda:

[&](){ ...your code... }(); // immediately executed lambda expression

在功能上等同于

{ ...your code... } // simple code block

这使得lambda表达式成为重构复杂函数的强大工具。首先在lambda函数中包装一个代码段,如上所示。然后,显式参数化过程可以在每个步骤之后通过中间测试逐步执行。一旦代码块完全参数化(如删除&所示),就可以将代码移动到外部位置并使其成为正常函数。

类似地,您可以使用lambda表达式根据算法的结果初始化变量。。。

int a = []( int b ){ int r=1; while (b>0) r*=b--; return r; }(5); // 5!

作为划分程序逻辑的一种方法,您甚至可能会发现将lambda表达式作为参数传递给另一个lambda表达式是有用的。。。

[&]( std::function<void()> algorithm ) // wrapper section
   {
   ...your wrapper code...
   algorithm();
   ...your wrapper code...
   }
([&]() // algorithm section
   {
   ...your algorithm code...
   });

Lambda表达式还允许您创建命名的嵌套函数,这是避免重复逻辑的一种方便方法。当将一个非平凡函数作为参数传递给另一个函数时,使用命名的lambdas看起来也容易一些(与匿名内联lambdas相比)。注意:不要忘记右大括号后面的分号。

auto algorithm = [&]( double x, double m, double b ) -> double
   {
   return m*x+b;
   };

int a=algorithm(1,2,3), b=algorithm(4,5,6);

如果后续的分析揭示了函数对象的大量初始化开销,您可以选择将其作为普通函数重写。

lambda函数是您在线创建的匿名函数。它可以捕获一些已经解释过的变量(例如。http://www.stroustrup.com/C++11FAQ.html#lambda),但存在一些限制。例如,如果有这样的回调接口,

void apply(void (*f)(int)) {
    f(10);
    f(20);
    f(30);
}

您可以当场编写一个函数来使用它,就像下面传递给应用程序的函数一样:

int col=0;
void output() {
    apply([](int data) {
        cout << data << ((++col % 10) ? ' ' : '\n');
    });
}

但你不能这样做:

void output(int n) {
    int col=0;
    apply([&col,n](int data) {
        cout << data << ((++col % 10) ? ' ' : '\n');
    });
}

由于C++11标准的限制。如果您想使用捕获,则必须依赖库和

#include <functional> 

(或其他类似STL库的算法来间接获取),然后使用std::function,而不是像这样传递普通函数作为参数:

#include <functional>
void apply(std::function<void(int)> f) {
    f(10);
    f(20);
    f(30);
}
void output(int width) {
    int col;
    apply([width,&col](int data) {
        cout << data << ((++col % width) ? ' ' : '\n');
    });
}

C++作者Bjarne Stroustrup在其著作《C++编程语言》第11章(ISBN-13:978-0321563842)中给出了lambda表达式的最佳解释之一:

什么是lambda表达式?

lambda表达式,有时也称为lambda函数或(严格地说不正确,但口语上)作为lambda是定义和使用匿名函数对象的简化符号。而不是用运算符()定义一个命名类,然后创建该类的对象,最后调用它,我们可以使用速记。

我什么时候用?

当我们希望将操作作为算法的参数。在图形用户界面的上下文中(和其他地方),此类操作通常被称为回调。

他们解决了哪类在介绍之前不可能解决的问题?

在这里,我想用lambda表达式完成的每个操作都可以在没有它们的情况下解决,但需要更多的代码和更大的复杂性。Lambda表达式这是对代码进行优化的方法,也是使其更具吸引力的方法。正如Stroustup所言:

优化的有效途径

一些示例

通过lambda表达式

void print_modulo(const vector<int>& v, ostream& os, int m) // output v[i] to os if v[i]%m==0
{
    for_each(begin(v),end(v),
        [&os,m](int x) { 
           if (x%m==0) os << x << '\n';
         });
}

或通过功能

class Modulo_print {
         ostream& os; // members to hold the capture list int m;
     public:
         Modulo_print(ostream& s, int mm) :os(s), m(mm) {} 
         void operator()(int x) const
           { 
             if (x%m==0) os << x << '\n'; 
           }
};

甚至

void print_modulo(const vector<int>& v, ostream& os, int m) 
     // output v[i] to os if v[i]%m==0
{
    class Modulo_print {
        ostream& os; // members to hold the capture list
        int m; 
        public:
           Modulo_print (ostream& s, int mm) :os(s), m(mm) {}
           void operator()(int x) const
           { 
               if (x%m==0) os << x << '\n';
           }
     };
     for_each(begin(v),end(v),Modulo_print{os,m}); 
}

如果需要,可以将lambda表达式命名如下:

void print_modulo(const vector<int>& v, ostream& os, int m)
    // output v[i] to os if v[i]%m==0
{
      auto Modulo_print = [&os,m] (int x) { if (x%m==0) os << x << '\n'; };
      for_each(begin(v),end(v),Modulo_print);
 }

或者假设另一个简单的样本

void TestFunctions::simpleLambda() {
    bool sensitive = true;
    std::vector<int> v = std::vector<int>({1,33,3,4,5,6,7});

    sort(v.begin(),v.end(),
         [sensitive](int x, int y) {
             printf("\n%i\n",  x < y);
             return sensitive ? x < y : abs(x) < abs(y);
         });


    printf("sorted");
    for_each(v.begin(), v.end(),
             [](int x) {
                 printf("x - %i;", x);
             }
             );
}

将生成下一个

01.01.01.01.01.0排序x-1;x-3;x-4;x-5;x-6;x-7;x-33;

[]-这是捕获列表或lambda导入器:如果lambda不需要访问其本地环境,我们可以使用它。

书中引用:

lambda表达式的第一个字符始终是[.a lambda介绍人可以采取各种形式:•[]:空捕获列表。这暗示不能使用周围上下文中的本地名称在lambda车身中。对于此类lambda表达式,数据从参数或来自非局部变量。•[&]:隐式捕获参考可以使用所有本地名称。所有局部变量均为通过引用访问。•[=]:按值隐式捕获。所有本地可以使用名称。所有名称都引用本地变量的副本在lambda表达式的调用点获取。•[捕获列表]:显式捕获;捕获列表是要通过引用或值捕获(即存储在对象中)的本地变量的名称列表。名称前面带有&的变量由捕获参考其他变量由值捕获。捕获列表可以还包含this和后跟…的名称。。。作为元素。•[&,capture-list]:通过引用隐式捕获列表中未列出名称的所有局部变量。捕获列表可以包含此内容。列出的名称不能以&开头。中命名的变量捕获列表按值捕获。•[=,捕获列表]:通过值隐式捕获列表中未提及名称的所有局部变量。捕获列表不能包含此内容。列出的名称必须以&开头。捕获列表中命名的变量是通过引用捕获的。请注意,以&开头的本地名称总是由引用和本地名称不是由预先放弃的&总是由捕获价值仅通过引用捕获允许修改呼叫环境。

附加的

Lambda表达式格式

其他参考:

维基open-std.org,第5.1.2章

C++11引入了lambda表达式,允许我们编写一个内联函数,该函数可用于短代码片段

[ capture clause ] (parameters) -> return-type
{
   definition of method
}

通常,lambda表达式中的返回类型是由编译器自身计算的,我们不需要指定显式和->返回类型部分可以忽略,但在某些复杂情况下,如条件语句中,编译器无法确定返回类型,我们需要指定。

// C++ program to demonstrate lambda expression in C++
#include <bits/stdc++.h>
using namespace std;

// Function to print vector
void printVector(vector<int> v)
{
    // lambda expression to print vector
    for_each(v.begin(), v.end(), [](int i)
    {
        std::cout << i << " ";
    });
    cout << endl;
}

int main()
{
    vector<int> v {4, 1, 3, 5, 2, 3, 1, 7};

    printVector(v);

    // below snippet find first number greater than 4
    // find_if searches for an element for which
    // function(third argument) returns true
    vector<int>:: iterator p = find_if(v.begin(), v.end(), [](int i)
    {
        return i > 4;
    });
    cout << "First number greater than 4 is : " << *p << endl;


    // function to sort vector, lambda expression is for sorting in
    // non-decreasing order Compiler can make out return type as
    // bool, but shown here just for explanation
    sort(v.begin(), v.end(), [](const int& a, const int& b) -> bool
    {
        return a > b;
    });

    printVector(v);

    // function to count numbers greater than or equal to 5
    int count_5 = count_if(v.begin(), v.end(), [](int a)
    {
        return (a >= 5);
    });
    cout << "The number of elements greater than or equal to 5 is : "
        << count_5 << endl;

    // function for removing duplicate element (after sorting all
    // duplicate comes together)
    p = unique(v.begin(), v.end(), [](int a, int b)
    {
        return a == b;
    });

    // resizing vector to make size equal to total different number
    v.resize(distance(v.begin(), p));
    printVector(v);

    // accumulate function accumulate the container on the basis of
    // function provided as third argument
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int f = accumulate(arr, arr + 10, 1, [](int i, int j)
    {
        return i * j;
    });

    cout << "Factorial of 10 is : " << f << endl;

    //   We can also access function by storing this into variable
    auto square = [](int i)
    {
        return i * i;
    };

    cout << "Square of 5 is : " << square(5) << endl;
}

输出

4 1 3 5 2 3 1 7
First number greater than 4 is : 5
7 5 4 3 3 2 1 1
The number of elements greater than or equal to 5 is : 2
7 5 4 3 2 1
Factorial of 10 is : 3628800
Square of 5 is : 25

通过从封闭范围访问变量,lambda表达式可以比普通函数更强大。我们可以通过三种方式从封闭范围中捕获外部变量:

通过引用捕获按价值捕获两者都捕获(混合捕获)

用于捕获变量的语法:

[&]:通过引用捕获所有外部变量[=]:按值捕获所有外部变量[a,&b]:通过值捕获a,通过引用捕获b带有空捕获子句[]的lambda只能访问其本地变量。

    #include <bits/stdc++.h>
    using namespace std;
    
    int main()
    {
        vector<int> v1 = {3, 1, 7, 9};
        vector<int> v2 = {10, 2, 7, 16, 9};
    
        // access v1 and v2 by reference
        auto pushinto = [&] (int m)
        {
            v1.push_back(m);
            v2.push_back(m);
        };
    
        // it pushes 20 in both v1 and v2
        pushinto(20);
    
        // access v1 by copy
        [v1]()
        {
            for (auto p = v1.begin(); p != v1.end(); p++)
            {
                cout << *p << " ";
            }
        };
    
        int N = 5;
    
        // below snippet find first number greater than N
        // [N] denotes, can access only N by value
        vector<int>:: iterator p = find_if(v1.begin(), v1.end(), [N](int i)
        {
            return i > N;
        });
    
        cout << "First number greater than 5 is : " << *p << endl;
    
        // function to count numbers greater than or equal to N
        // [=] denotes, can access all variable
        int count_N = count_if(v1.begin(), v1.end(), [=](int a)
        {
            return (a >= N);
        });
    
        cout << "The number of elements greater than or equal to 5 is : "
            << count_N << endl;
    }

输出:

   First number greater than 5 is : 7
   The number of elements greater than or equal to 5 is : 3

它解决了一个问题:对于使用输出参数函数初始化常量成员的构造函数调用,代码比lambda简单

您可以通过调用一个函数来初始化类的常量成员,该函数通过将其输出作为输出参数返回来设置其值。