C++11 Lambda表达式(匿名函数)
发布日期:2021-06-28 22:10:12 浏览次数:2 分类:技术文章

本文共 2717 字,大约阅读时间需要 9 分钟。

语法 序号
[ 捕获列表 ] ( 形参数列表 ) mutable(可选) throw()(异常属性) -> 返回值类型 { 函数体 } (1)
[ capture-list ] ( params ) -> ret { body } (2)
[ capture-list ] ( params ) { body } (3)
[ capture-list ] { body } (4)
(1)为完整的形式,包含变量捕获列表、形参列表、可变属性(可选)和返回值类型等。
(2)省略了mutable,表示Lambda不能修改捕获的变量。
(3)Lambda的返回值类型如果可以由函数体中的实际返回值推导出,可以省略。
(4)如果没有形参,可以省略圆括号。
mutable 修饰符说明 lambda 表达式体内的代码能够改动被捕获的变量。而且能够訪问被捕获对象的 non-const 方法。
exception 说明 lambda 表达式是否抛出异常(noexcept)。以及抛出何种异常,类似于void f()throw(X, Y)。
另外,capture 指定了在可见域范围内 lambda 表达式的代码内可见得外部变量的列表。详细解释例如以下:
[a,&b] a变量以值的方式呗捕获,b以引用的方式被捕获。
[this] 以值的方式捕获 this 指针。
[&] 以引用的方式捕获全部的外部自己主动变量。
[=] 以值的方式捕获全部的外部自己主动变量。
[] 不捕获外部的不论什么变量。
例子
   int x = 10;
    int y = 3;
    int z ;
    z = [=]()mutable throw() -> int { int n = x + y; x = y ; y = n; return n;}();
    cout<<z<<endl;
    cout<<"x:"<<x<<"\t"<<"y:"<<y<<endl;
运行结果为:
13
x: 10  y: 3
因为是以值传递的方式访问x,y所以x,y的值并没有发生改变
#include <iostream>
using namespace std;
int main()
{
   int n = [] (int x, int y) { return x + y; }(5, 4);
   cout << n << endl;
}
运行结果为:9
通过这个例子我们可以看出,通过“函数体”后面的‘()’传入参数。
接下来这个例子可以看出,可以像调用函数一样使用lambda表达式,但是感觉这种方式和普通函数的定义与调用就差不多了,这里只是学习使用方式而已。
#include <iostream>
using namespace std;
int main()
{
   auto f = [] (int x, int y) { return x + y; };
   cout << f(21, 12) << endl;
}
复制代码
运行结果为:33
#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);
}

转载地址:https://blog.csdn.net/yhc166188/article/details/79298035 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:move(),remove()和remove_if()
下一篇:c++11 std::bind std::function

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月23日 09时05分39秒