Introduction to C + + lambda expressions

Source: Internet
Author: User

Lambda expression

The primary purpose of the C++11 standard lambda introduction is to:

A function-like expression can be used as a function parameter that accepts a function pointer or function, so a typical lambda expression is a test expression or a comparison expression that can be written as a statement, making Lambda simple and easy to understand.

1. A lambda expression has the following types:

[Capture List] (parameter list)->returntype{function body}

Capture list is a list of local function variables defined in a function that contains a lambda, return type, parameter list, and function list, respectively, representing return types, argument lists, and body of functions.

2. Lambda Capture and return

(1), Value capture

size_tv1=23;

Autof2=[v1]{return v1;}

v1=0;

Autoj=f ();//j=23

(2), reference capture

size_tv2=34;

Autof2=[&v2]{return v2;}

v1=0;

Autoj=f ();//j=0

(3), implicit capture

W=find_if (Q.begin (), Q.end (), [=] (conststring &s) {return s.size () >=sz;}) = Capture for value,& for reference

(4), variable lambda

If you want to change the captured variable, you must precede the parameter with mutable, as in the following example:

size_tv1=23;

AUTOF2=[V1] () Mutable{return ++v1;}

v1=0;

Autoj=f ();//j=23//j=24

size_tv2=34;

Autof2=[&v2]{return ++v2;}

v1=0;

Autoj=f ();//j=1

Capture List of lambda

[]-------An empty capture list, lambda cannot use the function variable in which it is located, and a lambda can only use it after capturing the variable;

[Names]--------names is a comma-separated list of names;

[&]----------An implicit capture list, using a reference capture method;

[=]-----------An implicit capture list, using the value capture method;

Use the sample code:

Use of/*LAMBDA expressions */#include <iostream> #include <vector> #include <cmath> #include <algorithm># Include<ctime>using namespace Std;void lambda () {vector<int> numbers (Size), Srand (Time (0)); Generate (Numbers.begin (), Numbers.end (), Rand);//Generate a random number and join the container cout<< "size=" <<numbers.size () << ' \ n ';//the size of the container int count3=count_if (Numbers.begin (), Numbers.end (), [] (int x) {return x%3==0;}); The/LAMBDA expression implements the number of computations divisible by 3 cout<< "count3=" <<count3<< ' \ n '; int Count13=0;for_each (Numbers.begin (), Numbers.end (), [&count13] (int x) {count13+=x%13==0;}); cout<< "count13=" <<count13<< ' \ n '; Count3=count13=0;for_each (Numbers.begin (), Numbers.end (), [ &] (int x) {count3+=x%3==0;count13==x%13==0;}); cout<< "count3=" <<count3<< ' \ n '; cout<< "count13=" <<count13<< ' \ n ';} int main () {lambda (); return 0;}


(End of full text)

Introduction to C + + lambda expressions

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.