The latest version of C + +, C++14 was adopted in August 2014. C++14 brings some long-awaited changes, such as the auto type can be used as a function return value type, and a generic lambda expression--also the subject of this article.
Lambda in C + + is described in the C++11 standard. The main purpose is to write more concise, there is an anonymous function to replace the function object, avoid creating a separate class and a function definition. Here is a typical example of a c++11 lambda usage: Returns the square of a number.
int result = [] (int input) {return input * input;} (;cout<<) result << Endl;
If you want to reuse this code in multiple places, you can save the function in a variable:
Auto Func = [] (int input) {return input * input;};//first Usestd::cout << func (Ten) << std::endl;//second U Sestd::cout << func (23°c) << Std::endl;
Can you see the problem in the code above? It can only be used to calculate the square of an integer number. However, we would like to have this lambda expression applied to a double type or to a complex number. What we want is the following:
Square of an intstd::cout << func (Ten) << std::endl;//Square of a doublestd::cout << func (2.345) < < std::endl;//square of a complex numberstd::cout << func (std::complex<double> (3,-2)) << Std::endl ;
Obviously, we can use the function template:
Template <typename t>t func (T z) { return z * z;}
However, the way to define a well-known, global function is not what we are going to say in this article, the C++14 standard describes a generalized lambda expression that allows us to use auto as the parameter type of a lambda expression. We can write a more concise and elegant code:
Auto Func = [] (auto input) {return input * input;};
The following is the complete procedure for the above example:
#include <iostream> #include <complex>int main () { //Store A generalized lambda, that's squares a number, in a Variable auto func = [] (auto input) {return input * input;}; Usage Examples: //square of an int std::cout << func (Ten) << Std::endl; Square of a double std::cout << func (2.345) << Std::endl; Square of a complex number Std::cout << func (std::complex<double> (3,-2)) << Std::endl; return 0;}
The code above can be compiled with the current C + + compiler, for example: GCC 4.9.x or Clang. Here are the results of compiling and running with Clang, GCC:
$ clang++-std=c++1y-pedantic-wall-stdlib=libc++ test_01.cpp-o test_01$./test_011005.49903 (5,-12) $ g++-4.9.1-std=c+ +14-pedantic-wall test_01.cpp-o test_01$./test_011005.49903 (5,-12) $
However, lambda expressions also have a lot of sparkle in the STL. If we want to sort the numbers in a vector in descending order, we can use lambda expressions like this:
Std::sort (V.begin (), V.end (), [] (Auto I, auto j) {return (i > J);});
The following is a complete program that uses lambda to sort a vector with 10 integer numbers in descending order:
#include < iostream> #include <vector> #include <numeric> #include <algorithm>int main () {Std::vector<int > V (10); Use Std::iota to create a sequence of integers 0, 1, ... Std::iota (V.begin (), V.end (), 1); Print the unsorted data using Std::for_each and a lambda std::cout << "Original data" << Std::endl; Std::for_each (V.begin (), V.end (), [] (auto i) {std::cout << i << "";}); Std::cout << Std::endl; Sort the data using Std::sort and a lambda Std::sort (V.begin (), V.end (), [] (Auto I, auto j) {return (i > J);}) ; Print the sorted data using Std::for_each and a lambda std::cout << "sorted data" << Std::endl; Std::for_each (V.begin (), V.end (), [] (auto i) {std::cout << i << "";}); Std::cout << Std::endl; return 0;}
The following is the result of the above program operation:
$ g++-4.9.1-std=c++14-pedantic-wall test_02.cpp-o test_02$./test_02original data1 2 3 4 5 6 7 8 9 10Sorted data10 9 8 7 6 5 4 3 2 1$
If you are interested in understanding c++11 grammar, then I recommend you read this book: Jarne Stroustrup's "the C + + programming Language", or this: "Professional C + +"
Original link: https://solarianprogrammer.com/2014/08/28/cpp-14-lambda-tutorial/
C++14 Lambda Introduction