Lambda expressions in C ++, and lambda expressions in C ++

Source: Internet
Author: User

Lambda expressions in C ++, and lambda expressions in C ++

C ++ 11 also supports Lambda expressions, namely anonymous functions.

First, let's look at an example to give a perceptual knowledge of Lambda expressions:

# Include <iostream> # include <vector> # include <algorithm> using namespace std; int main () {int count = 10; vector <int> nums (count, 1 ); int x = 1, y = 1; // The lambda expression in the generate_n function call assigns an element in the vector object to the sum of generate_n (nums. begin () + 2, count-2, [=] () mutable-> int {int n = x + y; x = y; y = n; return n ;}); // for_each outputs the element for_each (nums. begin (), nums. end (), [] (int num) {cout <num <'';}); cout <endl; return 0 ;}

The result is as follows:



The syntax of Lambda expressions in C ++ is as follows:


Where,

Lambda introducer
[Lambda-introducer] identifies the beginning of a Lambda expression. This part must exist and cannot be omitted. Parameters in lambda-introducer are passed to the constructor of the function object class automatically generated by the compiler. Function object parameters can only use local variables that are visible within the scope of Lambda when Lambda is defined (including this of the class where Lambda is located ). Function object parameters take the following form:
1. []: no object parameters are used.
2. [=]: All visible local variables (including this of the class where Lambda is located) can be used in the function body ), and it is the value transfer method (equivalent to the compiler automatically passing all local variables by value ).
3. [&]: All visible local variables (including this of the class where Lambda is located) can be used in the function body ), and it is the reference transfer method (equivalent to the compiler automatically passing all local variables for us by reference ).
4. [this]: The member variables in the class where Lambda is located can be used in the function body.
5. [a]: pass a by value. When passing by value, the function body cannot modify the copy of a passed in, because the function is const by default. To modify the copy of a passed in, you can add a mutable modifier.
6. [& a]: pass a by reference.
7. [a, & B]: transfers a by value, and B by reference.
8. [=, & a, & B]: All parameters except a and B are passed by reference.
9. [&, a, B]: All parameters except a and B are passed by reference.

Parameter List(If not, it can be omitted)
The parameter list is the same as the common function parameter list and is enclosed. Parameters can be passed by value (such as (a, B) and by reference (such as (& a, & B. If no parameter exists, the parameter list can be omitted.

Mutable and exception Declaration(Optional)
Mutable or exception declaration, which can be omitted. When passing function object parameters by value with the mutable modifier, you can modify the copy passed by value (note that the copy can be modified, rather than the value itself ). The exception declaration is used to specify the exceptions thrown by the function. For example, throw (int) can be used to throw an integer exception ).

Return type
-> Return value type, which identifies the type of the function return value. When the return value is void or the function body contains only one return field (in this case, the compiler can automatically infer the return value type, this part can be omitted.

Lambda subject
{Function body} indicates the implementation of the function. This part cannot be omitted, but the function body can be empty.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.