We know that lambda expressions behave much like an anonymous function, and we often use lambda expressions in standard algorithms. For example, you need to print a vector, which may be written like this:
vector<int> v(1010);std::for_each(v.begin(), v.end(), [](int n){ cout" ";});
Using the for_each algorithm to use lambda expressions for each element in V, and successfully achieved the goal.
But look at the following way to accomplish the task:
std::for_each(v.begin(), v.end(), print_int);
What is the print_int here? Most people will know that it is a void print_int(int) function of the shape, yes, but he can also be a different type, which means it is not a function.
When learning the operator overloads of a class, many people may ignore () overloads, which are the overloads of parentheses. We know that overloading the parentheses of a class allows you to use the class object like a function. Examples are as follows:
class Print{ voidoperator()(int n){ cout" "; }};Print print_int; //声明类对象print_int(3); //使用了()运算符
So we'll print out the 3. So the print_int in the For_each statement above can also be a class object. The purpose of this is simply to elicit the following topic: What is the nature of lambda expression?
In fact, in C + +, lambda expressions are translated by the compiler into an unnamed object of a class that is not named. The lambda expression above is translated into an unnamed class like the print class, whereas in For_each, an unnamed object is declared, and then the handle of each element in V is done using its bracket carriage.
We know that the [] front of the lambda expression is the capture list, and the capture method is captured by reference and captured by value. When referencing by value, the compiler uses this value as a class member variable when creating a class, and initializes the class member to the current value of the referenced variable when declaring an unnamed object.
When captured by reference, the compiler does not store it as a class member. Here is an example to illustrate the above.
int value = 100;auto it = find_if(v.begin(),v.end(),[value](const int n){ return n<value;});cout<<*it;
The above code is to find the first number less than 100 in V, where I pass the length of the maximum length to a lambda expression by value. The compiler may have the following class for it:
class XXX{public: XXX(int x):value(x){} //这里的x就是捕获列表中的变量 --- [value] booloperator()(constint n)const{ //这里与lambda的参数,返回值,函数体一致 return n<value; }private: intvalue;};
After that, we can do this:
intvalue100;XXX xx(value);auto it = find_if(v.begin(), v.end(), xx);cout<<*it;
There is no doubt that here find_if is called on every element in V xx.operator(int n) , by returning an iterator that is worth the first value less than value.
In fact, you can also use the XXX class:
auto it = find_if(v.begin(), v.end(), XXX(100));
Now that you might have some knowledge of lambda expressions, you might want to define a class like this to test if it really is what I'm saying here.
What is the essence of lambda in C + +?