The basic format for lambda expressions in C + + 11 is:
[Capture] (parameters)-return_type {/* ... */}
where [] The external variable is passed in:
[] //no variables defined. Attempting to use any external variables in the lambda was an error. [X, &y] X is captured by value, Y was captured by Reference[&] //any external variable was implicitly captured by Referenc E if used[=] //any external variable is implicitly captured by value if Used[&, X] //x is explicitly captured By value. Other variables would be captured by reference[=, &z] //z are explicitly captured by reference. Other variables'll is captured by value
() is a parameter, for example:
[] (int x, int y), int {return x + y;}
Return_type is the literal meaning, that is, the return type or function type. Inside {} is the function code that we write function to execute.
We know that to execute a function you need to call the function name, but the anonymous function does not have a function name (the biggest benefit of anonymous function is that after defining the function no longer tangled to the function of what name), so suddenly get a lambda expression, we will suddenly a bit confused, do not know how to execute it.
The following code gives several ways to invoke and execute an anonymous function:
#include <iostream> #include <vector> #include <string> #include <functional>/** function */# Include <algorithm>/** for_each *//** Establish a function similar to the usage of the Create thread function to complete the execution of anonymous functions */int my_eval (std::function <int (int, int) & Gt f, int x, int y) {return f (x, y);} int main () {/** usage 1: By storing the anonymous function in a variable and passing it as a parameter */std::function<int (int, int) > F0 = [] (int x, int y), int {return x + Y };//lambda expression Auto F1 = [] (int x, int y), int {return x * y;};//does not use any external variables std::cout << "F0:" << my_eval (F0, 3, 4) << std::endl;std::cout << "F1:" << my_eval (F1, 2, 3) << std::e ndl;/** Usage 2: Save to Vector */std::vector<decltype (F0) > func_s{F0, F1};func_s.push_back ([] (int x, int y) {return x-y; }); for (auto f:func_s)//Traverse anonymous function Std::cout << "F:" << F (3, 1) << std::endl;/** usage 3: Use For_each to pass in anonymous functions */STD ::vector<int> nums{1,2,3,4,5};int sum = 0;//code uses sum thus through the [&] Reference implicit capture, if the code does not use any external variables, the parameter Std::for_each is not passed (Begin (Nums), End (Nums), [&] (int x) {sum + = x;}); /x is the value passed for traversing nums std::cout << "F3:" << sum << std::endl;//, but the anonymous function parameter has a different place std::string str{"Hello World" };std::string s = ""; std::string c = "";//indicates that the external variable s is captured by reference, C is captured by passing the value Std::for_each (Begin (STR), End (str), [&s, C] (char x) {s + = (x + c);}); Std::cout << "F4:" << s << std::endl;/** usage 4: Save to function pointer */auto my_lambda_func = [] (int x) {std::cout <&L T "F5:" << x << Std::endl;}; void (*func_ptr) (int) = MY_LAMBDA_FUNC;FUNC_PTR (4);//callback return 0;}
Operation Result:
References:
Wikipedia-c++ 11
C + + 11--LAMBDA expressions (anonymous functions)