zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++11 Lambda表达式简单解析

C++ 简单 解析 11 表达式 lambda
2023-09-14 09:06:25 时间

C++11 新增了非常多特性,lambda 表达式是当中之中的一个。假设你想了解的 C++11 完整特性,

建议去http://www.open-std.org/看看新标准!


非常多语言都提供了 lambda 表达式,如 Python,Java 8

lambda 表达式能够方便地构造匿名函数,假设你的代码里面存在大量的小函数,而这些函数一般仅仅被调用一次。那么最好还是将他们重构成 lambda 表达式.

C++11 的 lambda 表达式规范例如以下:

[ capture ] ( params ) mutableexceptionattribute -> ret {body } (1)
[ capture ] ( params ) -> ret { body } (2)
[ capture ] ( params ) { body } (3)
[ capture ] { body } (4)

当中

  • (1) 是完整的 lambda 表达式形式。
  • (2) const 类型的 lambda 表达式,该类型的表达式不能改捕获("capture")列表中的值。

  • (3)省略了返回值类型的 lambda 表达式。可是该 lambda 表达式的返回类型能够依照下列规则推演出来:
    • 假设 lambda 代码块中包括了 return 语句,则该 lambda 表达式的返回类型由 return 语句的返回类型确定。
    • 假设没有 return 语句。则类似 void f(...) 函数。
  • 省略了參数列表,类似于无參函数 f()。

mutable 修饰符说明 lambda 表达式体内的代码能够改动被捕获的变量。而且能够訪问被捕获对象的 non-const 方法。

exception 说明 lambda 表达式是否抛出异常(noexcept)。以及抛出何种异常,类似于void f()throw(X, Y)

attribute 用来声明属性。

另外,capture 指定了在可见域范围内 lambda 表达式的代码内可见得外部变量的列表。详细解释例如以下:

  • [a,&b] a变量以值的方式呗捕获,b以引用的方式被捕获。

  • [this] 以值的方式捕获 this 指针。

  • [&] 以引用的方式捕获全部的外部自己主动变量。
  • [=] 以值的方式捕获全部的外部自己主动变量。
  • [] 不捕获外部的不论什么变量。

此外,params 指定 lambda 表达式的參数。

2个详细的 C++11 lambda 表达式样例:

复制代码
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
 
int main()
{
    std::vector<int> c { 1,2,3,4,5,6,7 };
    int x = 5;
    c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end());
 
    std::cout << "c: ";
    for (auto i: c) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
 
    // the type of a closure cannot be named, but can be inferred with auto
    auto func1 = [](int i) { return i+4; };
    std::cout << "func1: " << func1(6) << '\n'; 
 
    // like all callable objects, closures can be captured in std::function
    // (this may incur unnecessary overhead)
    std::function<int(int)> func2 = [](int i) { return i+4; };
    std::cout << "func2: " << func2(6) << '\n'; 
}


//配合boost库
#include <boost/function.hpp>
typedef boost::function<void(int)> fobject_t;

// Now this function may accept functional objects
void process_integers(const fobject_t& f);

#include <assert.h>
#include <deque>
int main() 
{
    // lambda function with no parameters that does nothing
    process_integers([](int /*i*/){});

    // lambda function that stores a reference
    std::deque<int> ints;
    process_integers([&ints](int i){
        ints.push_back(i);
    });

    // lambda function that modifies its content
    std::size_t match_count = 0;
    process_integers([ints, &match_count](int i) mutable {
        if (ints.front() == i) {
           ++ match_count;
        }
        ints.pop_front();
    });

    assert(match_count == 6);
}

void process_integers(const fobject_t& f) 
{
    static const int data[] = {1, 2, 3, 4, 5, 200, 0};
    std::for_each(data, data + 6, f);
}