lambda is a function object

  When we write a lambda, the expression compiler translate into an unnamed unnamed class of objects . An overloaded function in the class containing the lambda expression generated call operator.

Copy the code
vector<string> svec = {"aaaa", "bbb", "ccccc"};

class shorterString
{
public:
    bool operator()(const string &a, const string &b) {return a.size() < b.size();}
};

int main()
{
    //方式1
    stable_sort(svec.begin(), svec.end(), [](const string &a, const string &b) {return a.size() < b.size();});

    //方式2
    stable_sort(svec.begin(), svec.end(), shorterString());

    for (auto i : svec)
        cout << i << ends;
}
Copy the code

Lambda represent and capture the behavior of the corresponding class

  As we know, when the object is a lambda expression by referencing the variable capture, the programmer will be responsible for ensuring the implementation of the referenced when lambda does exist. Thus in the compiler can be used directly without reference to the class generated in lambda stored as data members.

  Instead, variables capturing the value is copied to the lambda. Thus, this type lambda variable generated must be established for each member of the captured data corresponding to the same time create a constructor, allowed to use the variable value is initialized to the captured data member. For example as follows:

Copy the code
int main ()
{
    vector<string> words = {"aaaa", "bbbbbb", "cccc"};
    size_t sz = 4;
 
   
    // Mode 1 - Use lambda way
    auto wc = find_if(words.begin(), words.end(), [sz](const string &a){return a.size() >= sz;});
 
   
    cout << (wc == words.begin()) << endl;
}
Copy the code
Copy the code
class sizeComp
{
public:
    sizeComp(size_t n) : sz(n){}
    bool operator()(const string &s) const{return s.size() >= sz;}
private:
    size_t sz;
};

int main()
{
    vector<string> words = {"aaa", "bbbbbb", "cccc"};

    //方式2 - 使用函数对象方式
    auto wc = find_if(words.begin(), words.end(), sizeComp(4));

    cout << (wc == words.begin()) << endl;
}
Copy the code

  We used shorterString different classes and, above this class contains a data member, and a constructor for initializing the member.

  Class free lambda expression generated default constructor, assignment operator and destructor default; if it contains a data member type default copy / move constructor usually depends on and can not be captured.

Copy the code
vector<string> svec = {"aaaa", "bbb", "ccccc"};

class shorterString
{
public:
    bool operator()(const string &a, const string &b) {return a.size() < b.size();}
};

int main()
{
    //方式1
    stable_sort(svec.begin(), svec.end(), [](const string &a, const string &b) {return a.size() < b.size();});

    //方式2
    stable_sort(svec.begin(), svec.end(), shorterString());

    for (auto i : svec)
        cout << i << ends;
}
Copy the code

Lambda represent and capture the behavior of the corresponding class

  As we know, when the object is a lambda expression by referencing the variable capture, the programmer will be responsible for ensuring the implementation of the referenced when lambda does exist. Thus in the compiler can be used directly without reference to the class generated in lambda stored as data members.

  Instead, variables capturing the value is copied to the lambda. Thus, this type lambda variable generated must be established for each member of the captured data corresponding to the same time create a constructor, allowed to use the variable value is initialized to the captured data member. For example as follows:

Copy the code
int main ()
{
    vector<string> words = {"aaaa", "bbbbbb", "cccc"};
    size_t sz = 4;
 
 
    // Mode 1 - Use lambda way
    auto wc = find_if(words.begin(), words.end(), [sz](const string &a){return a.size() >= sz;});
 
 
    cout << (wc == words.begin()) << endl;
}
Copy the code
Copy the code
class sizeComp
{
public:
    sizeComp(size_t n) : sz(n){}
    bool operator()(const string &s) const{return s.size() >= sz;}
private:
    size_t sz;
};

int main()
{
    vector<string> words = {"aaa", "bbbbbb", "cccc"};

    //方式2 - 使用函数对象方式
    auto wc = find_if(words.begin(), words.end(), sizeComp(4));

    cout << (wc == words.begin()) << endl;
}
Copy the code

  We used shorterString different classes and, above this class contains a data member, and a constructor for initializing the member.

  Class free lambda expression generated default constructor, assignment operator and destructor default; if it contains a data member type default copy / move constructor usually depends on and can not be captured.

Guess you like

Origin www.cnblogs.com/bootblack/p/11369033.html