c++11 lambda expression (19 articles c++11)

Source: Internet
Author: User

C++11 introduces lambda expressions that allow programmers to define anonymous functions, which are executed at once, making it easy to program and prevent others from accessing them.

The syntax for lambda expressions is described by:

This assumes that we have defined a lambda expression like this. Now, what do you mean by the numbered parts on the way?

    1. The introduction of lambda expressions, in ' [] ' can be filled in ' = ' or ' & ' to indicate the lambda expression "capture" (lambda expression in a certain scope can be accessed by the data) in what way captured, ' & ' means a reference; ' = ' indicates capture by value, unless specifically indicated.
    2. A list of parameters for a lambda expression
    3. Mutable identification
    4. Exception ID
    5. return value
    6. The "function" body, which is the actual action required by the lambda expression

Add the complete code snippet:

int x = 10;

int y = 3;

int z;

z = [=] () mutable throw () int {int n = x + y; x = y; y = n; return n;} ();

cout<<z<<endl;

cout<< "x:" <<x<< "\ T" << "y:" <<y<<endl;

The result of the operation is:

13

X:10 Y:3

Because x, y is accessed as a value, the value of x, Y is not changed

Now that we have some understanding of the basic syntax of lambda expressions in our team, here are a few examples.

First, this example shows how to pass parameters to a lambda expression:

#include <iostream>

using namespace Std;

int main ()

{

int n = [] (int x, int y) {return x + y;} (5, 4);

cout << n << Endl;

}

Operation Result: 9

As we can see through this example, the arguments are passed in by the ' () ' after the function body.

The next example shows that you can use a lambda expression like a function call, but it's almost like the definition and invocation of a normal function, just learning how to use it.

#include <iostream>

using namespace Std;

int main ()

{

Auto F = [] (int x, int y) {return x + y;};

cout << F (+) << Endl;

}

Operation Result: 33

Lambda expressions are used in conjunction with STL algorithms, which are often used to sort and output arrays when writing test code, and are more convenient by the following algorithms:

#include <iostream>

#include <algorithm>

#include <ctime>

using namespace Std;

int main ()

{

int a[10] = {0};

Srand (Time (NULL));

Generate (a,a+10,[] ()->int {return rand ()% 100;});

cout<< "before sort:" <<endl;

For_each (A, a+10, [&] (int i) {cout<< i << "";});

cout<<endl;

cout<< "after sort" <<endl;

Sort (a,a+10);

For_each (A, a+10, [&] (int i) {cout<< i << "";});

return 0;

}

Nesting of lambda expressions:

#include <iostream>

int main ()

{

using namespace Std;

int m = [] (int x)

{return [] (int y) {return y * 2;} (x) + 3; } (5);

cout << m << Endl;

}

Run Result: 13

The above code can be successfully compiled on both VC10 and VC11. The sense lambda expression is also a more interesting syntax and the first VC11 extension I've touched.

Http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438176.html

Http://www.cnblogs.com/zhuyp1015/category/370450.html

c++11 lambda expression (19 articles c++11)

Related Article

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.