lambda function

Some basic knowledge of lambda function is  here. A lambda is an ad-hoc, locally-scoped function. Basically, lambdas are syntactic sugar, designed to reduce a lot of the work required in creating ad-hoc functor classes. There are three parts of lambda function

[capture list] (parameter list) {function body}

https://i-blog.csdnimg.cn/blog_migrate/cd979a9ee3e7204be4082d4f61ea84fe.png

The brackets ([]) mark the declaration of the lambda; it can have parameters, and it should be followed by its body (the same as any other function).  One difference between lambdas and functions: lambda parameters can’t have defaults. The body of the lambda is just a normal function body, and can be arbitrarily complex (although,  it’s generally good practice to keep lambda bodies relatively simple).

A lambda is an object  and has a type and can be stored. However, the type of the lambda is only known by the compiler (since it is compiler-generated), so you must use auto for declaration instances of the lambda.

https://i-blog.csdnimg.cn/blog_migrate/452f11158d3b28334b73ab81ea07f518.png

Sometimes it’s useful and convenient to be able to access objects from the lambda’s containing scope – that is, the scope within which the lambda was defined. If you were writing your own functor you could do this by passing in the appropriate parameters to the constructor of the functor. C++ provides a convenient mechanism for achieving this with lambdas called ‘capturing the context’. The context of a lambda is the set of objects that are in scope when the lambda is called. The context objects may becaptured then used as part of the lambda’s processing. Capturing an object by name makes a lambda-local copy of the object,

https://i-blog.csdnimg.cn/blog_migrate/cde05115e7f84c8c4cd5a1bb058c08a0.png

Capturing an object by reference allows the lambda to manipulate its context. That is, the lambda can change the values of the objects it has captured by reference.

https://i-blog.csdnimg.cn/blog_migrate/f3577f6858c638c6a33ad96cf1a02c77.png

All variables in scope can be captured using a default-capture.  This makes available all automatic variables currently in scope.

https://i-blog.csdnimg.cn/blog_migrate/7dfbaaccd19d03e65dc55367dc366d43.png

We could also add a capture list to a lambda

https://i-blog.csdnimg.cn/blog_migrate/c3b42c4a863eab38df3a82c2361da94e.png

It is perfectly possible (and quite likely) that we may want to use lambdas inside class member functions. Remember, a lambda is a unique (and separate) class of its own so when it executes it has its own context. Therefore, it does not have direct access to any of the class’ member variables. To capture the class’ member variables we must capture the this pointer of the class. We now have full access to all the class’ data (including private data, as we are inside a member function).

https://i-blog.csdnimg.cn/blog_migrate/d9bdebf77af8b3d4728341828ca4a6d4.png

One concrete example is given by

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main(){
    // Create a vector object that contains 10 elements.
    vector<int> v;
    for (int i = 1; i < 10; ++i) {
        v.push_back(i);
    }
    // Count the number of even numbers in the vector by
    // using the for_each function and a function object.
    int evenCount = 0;
    for_each(v.begin(), v.end(), [&evenCount](const int& item){
                if (item%2==0) evenCount++;
             });

    // Print the count of even numbers to the console.
    cout << "There are " << evenCount << " even numbers in the vector." << endl;
}

Lambda functions are usually used along with STL algorithms,

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main(){
    // Create a vector object that contains 10 elements.
    vector<int> v;
    for (int i = 1; i < 10; ++i) {
        v.push_back(i);
    }

    // Find all even numbers
    vector<int> evennumber;
    auto pos = find_if(v.begin(), v.end(), [](const int& item){return item%2==0;});
    while(pos!=v.end()){
        evennumber.push_back(*pos);
        pos++;
        pos = find_if(pos, v.end(), [](const int& item){return item%2==0;});
    }

    // Print the count of even numbers to the console.
    cout << "There are " << evennumber.size() << " even numbers in the vector." << endl;
    for(pos=evennumber.begin();pos!=evennumber.end();++pos){
        cout << *pos << " ";
    }
    cout << endl;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值