C++11のlambd表达式

在其他语言中,我们常见lambda表达式,c++11中也引入了。

利用Lambda表达式,可以方便的定义和创建匿名函数。今天,我们就来简单介绍一下C++中Lambda表达式的简单使用。

一、lambda的声明

完整声明如下:

[capture list] (params list) mutable exception-> return type { function body }

各项具体含义如下

  1. capture list:捕获外部变量列表
  2. params list:形参列表
  3. mutable指示符:用来说用是否可以修改捕获的变量
  4. exception:异常设定
  5. return type:返回类型
  6. function body:函数体

简略形式

1、    [capture list] (params list) -> return type {function body}
2、   [capture list] (params list) {function body}
3、   [capture list] {function body}
  • 格式1声明了const类型的表达式,这种类型的表达式不能修改捕获列表中的值。
  • 格式2省略了返回值类型,但编译器可以根据以下规则推断出Lambda表达式的返回类型: (1):如果function body中存在return语句,则该Lambda表达式的返回类型由return语句的返回类型确定; (2):如果function body中没有return语句,则返回值为void类型。
  • 格式3中省略了参数列表,类似普通函数中的无参函数。

    讲了这么多,我们还没有看到Lambda表达式的庐山真面目,下面我们就举一个实例。

  二、实例

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

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(int a,int b)
{
    return a < b;
}


int main()
{
    vector<int> m_vector{6,9,7,5};
    vector<int> m_vector2{ 8,11,7,5,6,9 };
    sort(m_vector.begin(), m_vector.end(),cmp);   //通常做法

    for (int it:m_vector)
    {
        cout << it << " ";
    }
    cout << endl;


    sort(m_vector2.begin(), m_vector2.end(), [](int a, int b)->bool {return a < b; });  //lambda表达式
    for (int it : m_vector2)
    {
        cout << it << " ";
    }
    cout << endl;

    getchar();
    return 0;
}

三、捕获变量说明

  • []                    什么也不捕获
  • [&]                  捕获所有变量,以引用方式
  • [=]                  捕获所有变量,以值的形式
  • [=, &foo]        除了foo以引用的形式捕获,其余以值形式捕获
  • [a]                 以值的形式捕获a
  • [&a]               以引用的形式捕获a
  • [this]             以copy形式捕获this指针
  • [*this]           通过传值方式捕获当前对象
  • [=,&]             以混合形式捕获

  在Lambda表达式中,如果以传值方式捕获外部变量,则函数体中不能修改该外部变量,否则会引发编译错误。那么有没有办法可以修改值捕获的外部变量呢?这是就需要使用mutable关键字,该关键字用以说明表达式体内的代码可以修改值捕获的变量,示例:

    int a = 1;
    auto x = [a]()mutable { cout << ++a; }; 
    cout << a << endl;

四、lambda的参数

Lambda表达式的参数和普通函数的参数类似,那么这里为什么还要拿出来说一下呢?原因是在Lambda表达式中传递参数还有一些限制,主要有以下几点:

  1. 参数列表中不能有默认参数
  2. 不支持可变参数
  3. 所有参数必须有参数名

 

     int m = [](int x) { return [](int y) { return y * 2; }(x)+6; }(5);           
        std::cout << "n:" << [](int x, int y) { return x + y; }(5, 4) << std::endl;                    
        auto gFunc = [](int x) -> function<int(int)> { return [=](int y) { return x + y; }; };
        auto hFunc = [](const function<int(int)>& f, int z) { return f(z) + 1; };int a = 111, b = 222;
        auto func = [=, &b]()mutable { a = 22; b = 333; std::cout << "a:" << a << " b:" << b << std::endl; };
        auto func2 = [=, &a] { a = 444; std::cout << "a:" << a << " b:" << b << std::endl; };
        auto func3 = [](int x) ->function<int(int)> { return [=](int y) { return x + y; }; };    
     std::function<void(int x)> f_display_42 = [](int x) { print_num(x); };
      f_display_42(44);

五、lambda表达式nutanle和exception的声明

按值传递函数对象参数时,加上 mutable 修饰符后,可以修改传递进来的拷贝(注意是能修改拷贝,而不是
值本身)。exception 声明用于指定函数抛出的异常,如抛出整数类型的异常,可以使用 throw(int)。

六、返回结果

标识函数返回值的类型,当返回值为 void,或者函数体中只有一处 return 的地方(此时编译器可以自动推断出返回值类型)
时,这部分可以省略。

七、lamnda的应用

1、直接调用

suto lam=[](){cout<<"hello world";}

lam();

2、签名,作参数传递

原文地址:https://www.cnblogs.com/xietianjiao/p/10491339.html