In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to use the newly introduced lambda expression in Clover 11, which is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
A highlight of the ISO C++ 11 standard is the introduction of Lambda expressions. The basic syntax is as follows:
[capture list] (parameter list)-> return type {function body}
Briefly talk about the function of each part.
1. [capture list] capture the list and capture it into the function body, so that the function body can access the
2. (parameter list) Parameter list, which is used to represent the parameter list of lambda expressions
3-> the return value {function body} of the return type function is the function body.
The lambda expression can be understood as an anonymous function (but it is not). If you want to use the function declared by the lambda expression, you need to "name" it.
Lambda expressions can represent closures because they are the class itself
Closures are blocks of code that can contain free variables (not bound to a specific object: a chestnut std::function can produce an object, or a function pointer that does not point to any function).
The closures are more popular and have the following points.
1. A function with its own context, the closure can store the context needed by the runtime, so that the closure can be used when the context does not exist (the variable a life cycle is destroyed, but can still be used in the closure)
two。 You can think of a closure as a class that overloads operator (), with the meaning of state being interpreted as using member variables through this pointers.
3.capture list is the way lambda expressions implement closures.
An example of simple use
Clocking 11 provides new features for auto, such as his name, which can now be regarded as an automatic adaptation type and can be adapted to most types.
Use auto instead of the type of variable, provided that it is initialized by an initialization variable of an explicit type, and you can use the auto keyword
Auto f = [] () {}; auto f = [] (int a, int b)-> int {return a + b;}; f (1,2); / / this is required
You can use this lambda expression as long as it is a function type
Typedef int (* FUNC) (int a, int b); int main () {FUNC a = [] (int a, int b) {return a + b;}; printf ("% d\ n", a (1,2));}
Methods that declare functions can receive lambda expressions without a capture list
Typedef std::function FUNC; int main () {FUNC a = [] (int a, int b) {return a + b;}; printf ("% d\ n", a (1,2));}
The use of capture list in lambda expressions
Int func (int a, int b, std::function f) {return f (a, b);} int astat1 Int bread2; int cymb3; int d = func (a, b, [a, b] (int m, int n) {printf ("a =% d\ n", a); / / an is captured by value transfer, and mutable only modifies the valid printf ("b =% d\ n", b) in the function body. / b is a reference pass-through capture, and mutable can affect external b / / printf ("c =% d\ n", c); / / c inaccessible return m + n;}); typedef int (* FUNC) (int m, int n std typedef int function f); void test () {FUNC oho; int a = 10; int b = 20 Auto func = [& a, & b] (int m, int n) {printf ("av% d bv% d\ n", a, b); return m + n;};}
1. [] empty. No function object parameters are used.
2. [=]. The body of the function can use all visible local variables within the scope of Lambda (including the this of the class where Lambda belongs), and it is the way of passing values (equivalent to the compiler automatically passing all local variables by value for us).
3. [&]. The function body can use all visible local variables within the scope of Lambda (including the this of the class where Lambda belongs), and is reference passing (equivalent to the compiler automatically passing all local variables by reference for us).
4. [this] . The body of the function can use the member variables in the class where the Lambda is located.
5. [a] . Pass a by value. When passing by value, the copy of the passed a cannot be modified in the function body, because the function is const by default. To modify the copy of the passed a, you can add the mutable modifier.
6. [& a]. Pass a by reference.
7. [a, & b]. Pass a by value and b by reference.
8. [=, & a, & b]. Except that an and b are passed by reference, other parameters are passed by value. Note that the position of the = symbol must be at the first
9. [&, a, b]. Except that an and b are passed by value, all other parameters are passed by reference. & the position of the symbol must be at the first
Add mutable when you want to change a variable captured by passing a value
[a, & b, & b2] (int m, int n) mutable {a * = 2; return masks;}:
Other uses of lambda expressions
Class A {public: a (); ~ A (); void test () {auto f = [this] (int m, int n) {printf ("% d\ n", a);};} private: int a;}
Lambda expression is essentially a type of closure, although it can assign values to function pointers, but only when the capture list is empty, when the capture list has a value, you should use auto to receive lambda expressions, or you can use std::function.
Main::__l2::
/ / this is the type of lambda expression displayed in the VS2015 environment. What can be used to receive and call him without auto?
Essentially, assignment is not allowed between lambda expressions.
Auto a = [] (int m, int n) {return m + n;}; auto b = [] (int m, int n) {return m-n;}; a = b
The operation is illegal because assignment operators are not allowed for closure types, but function pointers can, that is, have the following operations
Typedef int (* FUNC) (int a, int b); int main () {FUNC a = [] (int a, int b) {return a + b;}; FUNC b = [] (int a, int b) {return a + b;}; a = b; return 0;}
Std::function can also be assigned, so he can assign values with lambda expressions of capture list.
Typedef std::function FUNC; int m = 10; int n = 20; FUNC a = [m, n] (int a, int b) {printf ("% d\ n", m); return a + b;}; FUNC b = [m, n] (int a, int b) {return a + b;}; b = a; b (1,2) / / the result of the execution is that the above content can be printed out by m, which is how to use the newly introduced lambda expression in Category 11. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.