码迷,mamicode.com
首页 > 编程语言 > 详细

C++——Lambda表达式

时间:2019-09-13 22:29:35      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:语句   insert   auto   传统   begin   end   orm   word   hold   

0.使用场景---只有一两个地方使用的简单操作

独立出来一个函数,但这个函数实现相对简单并且可能在整个项目只使用了一次(即不存在复用的情况),那么这个时候我们就可以考虑使用下lambda表达式了。

?既然只使用一次,那直接写全代码不久醒了,为啥要函数呢?——因为lambda可以捕获局部变量

bool check_size(const string &s, string::size_type sz)
{
    return s.size() >= sz;
}

//wc:第一个满足size>sz的元素 auto wc
= find_if (words.begin(),words.end(),[sz](const string &a) {return a.size()>=sz;});

函数check_size()无法作为find_if的参数。而且需要考虑如何

 

参考:https://blog.csdn.net/qq_34199383/article/details/80469780

0.1 比较大小

//1.传统方法
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool compare(int& a, int& b)
{
    return a > b;
}

int main(void)
{
    int data[6] = { 3, 4, 12, 2, 1, 6 };
    vector<int> testdata;
    testdata.insert(testdata.begin(), data, data + 6);
    // 排序算法
    sort(testdata.begin(), testdata.end(), compare);    // 升序

    return 0;
}
/*******************************************/
//2.lambda表达式
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(void)
{
    int data[6] = { 3, 4, 12, 2, 1, 6 };
    vector<int> testdata;
    testdata.insert(testdata.begin(), data, data + 6);

    sort(testdata.begin(), testdata.end(), [](int a, int b){ return a > b; });

    return 0;
}

0.2 auto 和function 接受lambda表达式的返回

#include <iostream>
#include <functional>
using namespace std;

int main(void)
{
    int x = 8, y = 9;
    auto add = [](int a, int b) { return a + b; };
    std::function<int(int, int)> Add = [=](int a, int b) { return a + b; };

    cout << "add: " << add(x, y) << endl;//17
    cout << "Add: " << Add(x, y) << endl;//17

    return 0;
}

0.3 使用lambda表达式来实现递归算法

#include <iostream>
#include <functional>
using namespace std;

int main(void)
{
    std::function<int(int)> recursion = [&recursion](int n) { 
        return n < 2 ? 1 : recursion(n - 1) + recursion(n - 2); 
    };

    // 我们来检测下我们的结果
    cout << "recursion(2):" << recursion(2) << endl;//2
    cout << "recursion(3):" << recursion(3) << endl;//3
    cout << "recursion(4):" << recursion(4) << endl;//5

    return 0;
}

 

1.本质:未命名的内联函数

[capture list] (parameter list) ->return type 
{ 
    function body
}
//capture list 捕获列表,函数中定义的局部变量列表,通常为空
//lambda表达式必须使用尾置返回——return **

2.可以忽略返回类型,参数列表,但必须包含捕获列表和函数体

auto f =[] {return 42;}

3.捕获列表

auto wc = find_if(words.begin(), words.end(),[sz](const string &a) { return a.size() >= sz;} );

 4.变量截取的方式

  • [] 不截取任何变量
  • [&] 截取外部作用域中所有变量,并作为引用在函数体中使用
  • [=] 截取外部作用域中所有变量,并拷贝一份在函数体中使用
  • [=, &foo] 截取外部作用域中所有变量,并拷贝一份在函数体中使用,但是对foo变量使用引用
  • [bar] 截取bar变量并且拷贝一份在函数体重使用,同时不截取其他变量
  • [this] 截取当前类中的this指针。如果已经使用了&或者=就默认添加此选项。

 

参考:

https://www.cnblogs.com/smiler/p/4095723.html

 

5.返回类型:单一的return语句;多语句则默认返回void;否则报错,应指定返回类型

//正确,单一return语句
transform(vi.begin(),vi.end(),vi.begin(), [] (int i) { 
    return i<0? -i; i;});

//错误。不能推断返回类型
transform(vi.begin(),vi.end(),vi.begin(), [] (int i) { 
    if (I<0) return -i;
    else return i;})

//正确,尾置返回类型
transform(vi.begin(),vi.end(),vi.begin(), [] (int i) ->int
{     if (I<0) return -i;
    else return i;})

 

6.bind()

7.using std::placeholders::_1;

C++——Lambda表达式

标签:语句   insert   auto   传统   begin   end   orm   word   hold   

原文地址:https://www.cnblogs.com/yrm1160029237/p/11494965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!