/*
New features for C + +:
Lambda expression: is one of the final features of the C + + feature that actually provides
An attribute that resembles an anonymous function, while an anonymous function is a function that is required
, but do not want to bother to name a function in the case of use.
[Capture List] (parameter list) mutable (optional) Exception property, return type {
function body
}
The above syntax rules are well understood in addition to what is in [Capture list],
Just the function name of the general function is filtered out, and the return value is used in the form.
The so-called capture list can actually be understood as a type of parameter, lambda expression
The inner function body is not able to use variables outside the body of the function by default,
This time the capture list can act as an external data transfer. Depending on the behavior passed,
The capture list is also divided into the following categories:
1, value capture: Similar to parameter values, the early variables of the value capture can be copied, different
Is that the captured variable is copied when the lambda expression is created, not when it is called:
void Learn_lambda_func_1 () {
int value_1 = 1;
Auto Copy_value_1 = [value_1]{
return value_1;
} ;
value_1 = 100;
Auto Stored_value_1 = Copy_value_1 ();
At this time, stored_value_1 = = 1, while value_1 = = 100
Because, Copy_value_1 saved a copy of the value_1 when it was created.
We use Auto to get the type of Func, which is very important. Once you have defined the lambda function,
It can be used as a function.
Where [] means the next step is to define a lambda function, where there may be parameters in the middle of the brackets,
The following () is the function body in the middle of the parameter list {} of the lambda function.
The use of a lambda function: If you design a class for an address book. Now to provide a function query
This address book may be queried by name, may be queried based on address, and may be combined
, it would be cumbersome to write a function for all of these situations. So we should provide an interface, it is convenient for users to define their own
Query method.
*/
#include <iostream>
#include <string>
#include <vector>
Class addressbook{
Public
Template<typename func>
Std::vector<std::string> Findmatchaddress (func a)
{
Std::vector<str::string> Resaults;
for (Auto iter = _address.begin (); ITER! = _address.end (); ++iter)
{
if (func (*iter)) {
Resaults.push_back (*iter);
}
}
return resaults;
}
Private
std::vector<std::string>_address;
};
/*
We can see that the arguments provided by the findmatchaddresses function above are of type FUNCC,
This is a generic type. You should pass in a function during use, and then separate the
Each entry in the Address book executes this function if the return value is true so that indicates this
Entry meets the user's screening requirements, then it should be put into the results, then this func
How are the parameters of the type passed in?
*/
AddressBook Gloabl_address_book;
Std::vector<std::string> Findaddressesfromorgs () {
Std::vector<std::string> A;
[] (const std::string & addr) {
Return Addr.find (". org")! = Std::string::npos;
};
return A;
}
/*
As you can see, we define a lambda function directly when we call the function. The parameter types are:
Const string& Addr
The return value is of type bool.
If the user wants to query in a different way, just define a different lambda function.
*/
void Learn_lambda_func_1 () {
int value_1 = 1;
Auto Func = [] {
Std::cout << "Hello World" << Std::endl;
};
Func ();
Auto Copy_value_1 = [Value_1]
{
return value_1;
};
value_1 = 100;
Auto Store_value_1 = Copy_value_1 ();
Std::cout << store_value_1 << Std::endl;
Std::cout << value_1 << Std::endl;
Return
}
int main () {
Learn_lambda_func_1 ();
System ("pause");
std::string name;
Std::cin >> name;
Return gloabl_address_book.findmatchaddress{
[&] (Const std::string& STR) {
return Name.find (addr)! = String::npos;
};
}
return 0;
}
The variable reads in the/*LAMBDA function, the above sake,
The lambda function uses all the parameters of the function body and the information inside it.
No external information is used. We envision reading a name from the keyboard, and then
After you define an anonymous function with a lambda function, look in the Address book for any
People of the same name. Then this LAMBDAA function is bound to be able to use external
Variables in the block, so we have to use the variable intercept function.
(variable capture), as seen in the code above, our lambda function is already able to use an external scope
The variable in the name, one of the biggest differences between the lambda function is [] the & symbol is added to the middle,
This is to tell the compiler to do a variable intercept. This allows the lambda function body to use
External variables. If you do not add any symbols, the compiler will not take a variable intercept.
Here are the options for the various variables to intercept:
[] do not intercept
[&] intercepts all variables in the outer scope and uses them as references within the function body
[=] intercepts all variables in the outer scope and copies a copy to be used in the body of the function
[=, &foo] Intercepts all variables in the outer scope and copies a copy to be used in the function body, but uses a reference to the Foo variable
[bar] Intercept bar variable and copy one copy at function weight without intercepting other variables
[This] intercepts the this pointer in the current class. This option is added by default if you have already used & or =.
*/
/*
Lambda functions and STL
The introduction of lambda functions provides a great convenience for the use of STL,
using namespace Std;
V.push_back (1);
V.push_back (2);
//,,,
for (Auto iter = V.begin (); ITER! = V.end (); ++iter) {
cout << *iter << Endl;
}
Now that you have a lambda function, you can write this:
Vector<int> v;
V.push_back (1);
V.push_back (2);
//...
For_each (V.begin (), V.end (), [] (int val)) {
cout << Val << Endl;
}
*/
New features of C + + lambda