C ++ - Lambda Expressions

Simple 0. Using the Scene --- only one or two places used

Independent function, but the function is relatively simple and may use only once in the entire project (ie, multiplexing is not present), so this time we can consider the case of using a lambda expression.

? Since it is used only once, and that direct write all the code soon woke up, why should it function? - Because lambda can capture local variables

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;});

Function check_size () can not be used as a parameter find_if. But also need to consider how

 

Reference: https: //blog.csdn.net/qq_34199383/article/details/80469780

0.1 Comparison of size

//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 algorithm 
    Sort (testdata.begin (), testdata.end (), Compare);     // ascending 

    return  0 ; 
} 
/ * *** ************************************** * / 
// 2.lambda expression 
#include < the iostream> 
#include <Vector> 
#include <algorithm>
 the using  namespace STD; 

int main ( void ) 
{ 
    int Data [ . 6 ] = { . 3 , . 4 , 12 is ,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 and function to accept the return of lambda expressions

#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 expression is achieved using a recursive algorithm

#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. Essence: unnamed inline

[Capture List] (Parameter List) -> return type 
{ 
    function body 
} 
// Capture List capture list, a list of local variables defined in the usually empty
 // the lambda expression must set the end return --return **

2. You can ignore the return type of the parameter list, but the list must contain and capture function body

auto f =[] {return 42;}

3. capture list

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

 4. The variable manner taken

  • [] Not taken any variable
  • [&] Scope all variables taken outside and used as a reference in the body of the function
  • [=] Taken outside scope all variables, and a copy function in the body
  • [=, & Foo] taken outside scope all variables used in the copy function and the body, but the use of a reference variable foo
  • [Bar] bar taken and variables used in the copy function of body weight, while other variables not taken
  • [This] taken this pointer in the current class. If you have used default & or = to add this option.

 

reference:

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

 

5. Return type: single return statement; multi-statement the default returns void; otherwise an error, you should specify the type of return

// correct single return statement 
Transform (vi.begin (), vi.end (), vi.begin (), [] ( int I) { 
     return I < 0 -? I; I;}); 

// Error . Can not infer the return type 
Transform (vi.begin (), vi.end (), vi.begin (), [] ( int I) { 
     IF (the I < 0 ) return - I;
     the else  return I;}) 

// correct , the end of the return type is set 
Transform (vi.begin (), vi.end (), vi.begin (), [] ( int I) -> int 
{      IF (the I < 0 ) return - I;
     the else  return i;})

 

6.bind()

7.using std::placeholders::_1;

Guess you like

Origin www.cnblogs.com/yrm1160029237/p/11494965.html