C++11 Lambda-expression Learning

Source: Internet
Author: User

Lambda expressions are the basis of functional programming. I do not have enough understanding of functional programming, so here dare not nonsense, interested can find the relevant information on their own to see. This is just an introduction to the lambda expression in C++11 's own understanding. Here are the reference documents Http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2927.pdf

Given two examples, you can see how lambda expressions are written

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

[] (int x, int y), int {int z = x + y; return z + x;} Indicates that the return value type is int

A lambda expression is similar to the normal definition of a function, the difference is three points, one is no function name (you can also think of the function name is []), and the other is that the function does not have a normal function as the return value type, the return value type, that is, between the argument list and the function definition with an arrow, Three is before the parameter list there is a [] (actually there may be more form here)


The syntax for a lambda expression is as follows: [snap fast] (parameter) exception----return value type {principal} through Lambda expressions This feature allows you to write inline anonymous functions without having to write separate functions and function objects, making the code easier to understand. The following example of referencing someone will step through the use of lambda expressions:
Example one:
[]{std::cout<< "Hello from lambda!" <<std::endl;} ();
The output is as follows:
Hello from Lambda
In the console output hello from LAMBDA, the trailing parentheses make the expression available for immediate execution.
Example two:
std::string result=[] (const std::string& str)->std::string{return "Hello from" +STR;} ("Second Lambda");std::cout<<result<<std::endl;
The output is as follows:
Hello from Second Lambda
The lambda expression takes a string parameter and returns a string, and the result is saved in the variable result, and the trailing parenthesis causes the expression to execute immediately.
Example three:
Auto fn=[] (const std::string& str) {return "Hello from" +STR;}; STD::COUT&LT;&LT;FN ("Call 1") &LT;&LT;STD::ENDL;STD::COUT&LT;&LT;FN ("Call 2") <<std::endl;
The output is as follows:
Hello from call 1
Hello from Call 2
A pointer to a lambda expression is saved here, and the expression is executed through a function pointer.
Example four: (STL and Lambda)
#include <iostream> #include <algorithm> #include <vector> auto Main (int argc, char** argv) int{    Std::vector<int> Vec={1, 2, 3, 4, 5, 6, 7, 8, 9};    int value=3;    int cnt=std::count_if (Vec.cbegin (), Vec.cend (), [=] (int i) {return i>value;});     std::cout<< "Found" <<cnt<< "Values >" <<value<<std::endl; return 0;}
The output is as follows:
Found 6 Values > 3
Using the COUNT_IF algorithm to calculate the number of elements in a vector that satisfies a particular condition, the form of a lambda expression gives the condition, the expression is noted =, the equals sign is the variable that is captured by the value of the scope, and the value of value is captured in this example. The previous example [] is empty, that is, the catch block is empty, and the variable cannot be accessed within the body body of the lambda expression. Here's a detailed description of the snap block:
[=] Catch all variables by value
[&] Capturing all variables by reference
[value] to catch value by value, not to catch other variables
[&value] captures value by reference and does not catch other variables
[=, &value] Default by value snap, variable value exception, snap by reference
[&, value] default by reference SNAP, variable value exception, catch by value
Example five: (STL combined with Lambda)
#include <iostream> #include <algorithm> #include <vector> auto Main (int argc, char** argv) int{    Std::vector<int> vec={11, 22, 33, 44};    int index=0;             For_each (Vec.begin (), vec.end (), \ [&index] (int i) {std::cout<< "Value" << (index++) \     << ":" &LT;&LT;I&LT;&LT;STD::ENDL;}); return 0;}
The for_each algorithm allows you to perform specific operations on all elements in a given range, invoke a lambda expression, and pass the value as an argument to a lambda expression.
The sample program is almost there, and in c++11, the authorities seem to have encouraged everyone to use lambda expressions rather than function objects, and lambda expressions are easier to understand.

What are the advantages of lambda expressions in C + + compared to traditional functions?

Lambda expressions you can understand as anonymous functions, such as some small functions in your code, which are generally called only once (such as function pointers), so you can replace them with lambda expressions, so that the code looks more concise and easy to use.

C++11 Lambda-expression Learning

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.