An example of a LAMBDA expression

Source: Internet
Author: User

Turn from: https://msdn.microsoft.com/zh-cn/library/dd293599.aspx

This article demonstrates how to use a lambda expression in your program. For an overview of lambda expressions, see lambda expressions in C + +. For more information about the structure of lambda expressions, see lambda expression syntax. declaring a LAMBDA expression Example 1

Because the lambda expression is typed, you can assign it to the auto variable or function object as follows: code C + +

Declaring_lambda_expressions1.cpp
//compile with:/ehsc/w4
#include <functional>
#include < iostream>

int main ()
{

    using namespace std;

    Assign The lambda expression that adds two numbers to a auto variable.
    Auto F1 = [] (int x, int y) {return x + y;};

    cout << F1 (2, 3) << Endl;

    Assign the same lambda expression to a function object.
    Function<int (int, int) > F2 = [] (int x, int y) {return x + y;};

    cout << F2 (3, 4) << Endl;
}
Output
5
7
Note

For more information, see Automatic (c + +, function class, and function calls (C + +).

Although many lambda expressions are declared in the body of a function, they can be declared anywhere in the initialization variable. Example 2

The Visual C + + compiler binds the expression to the captured variable when it declares rather than invokes a lambda expression. The following example shows a lambda expression that captures the local variable I by value and captures the local variable J by reference. Because a lambda expression captures I through a value, reassigning I in the later part of the program does not affect the result of the expression. However, because a lambda expression captures J by reference, Reassigning J affects the result of the expression. Code C + +

Declaring_lambda_expressions2.cpp
//compile with:/ehsc/w4
#include <functional>
#include < iostream>

int main ()
{
   using namespace std;

   int i = 3;
   int j = 5;

   The following lambda expression captures I by value and
   //J by reference.
   Function<int (void) > f = [i, &j] {return i + j;};

   Change the values of I and J.
   i =;
   j =;

   Call F and print it result.
   cout << F () << Endl;
}
Output
47
Call LAMBDA expression

You can call the lambda expression immediately, as shown in the following code fragment. The second code fragment demonstrates how to pass a lambda as a parameter to the Standard Template Library (STL) algorithm, such as find_if. Example 1

The following example declares a lambda expression that returns the sum of two integers and calls the expression immediately using Parameters 5 and 4: code C + +

Calling_lambda_expressions1.cpp
//compile with:/EHsc
#include <iostream>

int main ()
{
   using namespace std;
   int n = [] (int x, int y) {return x + y;} (5, 4);
   cout << n << endl;
}
Output
9
Example 2

The following example passes a lambda expression as a parameter to the find_if function. Returns trueif the lambda expression has an even number of arguments. Code

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.