"1" Scenario analysis
Within a function, a piece of code may be used more than once, and the general case is to encapsulate the code in a number of times into a function.
However, if this code is used only in this function, then encapsulating the function is cumbersome and redundant.
So, is there a way to generate a " temporary function ", and the "temporary function" life cycle is only in this function? The C++11 can be implemented using function mates with lambda expressions.
"2" code example
A simple example is as follows:
1#include <iostream>2#include <functional>3 4 classA5 {6 Public:7 voidFuncold (intXinty)8 {9 //-----Code SectionTenAuto S1 = x +y; One //-----Code Section AAuto S2 = x +y; - //-----Code Section -Auto S3 = x +y; the -Std::cout <<"sum::"<< S1 + s2 + s3 <<Std::endl; - } - + voidFuncintXinty) - { +Auto FUNC1 = [ This](intXinty) A { at returnX +y; - }; - -Auto S1 =func1 (x, y); - -Auto S2 =func1 (x, y); in -Auto S3 =func1 (x, y); to +Std::cout <<"sum::"<< S1 + s2 + s3 <<Std::endl; - } the }; * $ voidMainintargcChar*argv[])Panax Notoginseng { - for(Auto C: {"Hello World!!!"})//c++17 for the { +Std::cout <<C; A } the +Std::cout <<Std::endl; - A; $A.func (2,3); $ -System"Pause"); - } the - //run out:Wuyi /* the Hello World!!! - sum:: Wu Please press any key to continue ... - */
Good good Study, day.
Sequential Selection Cycle Summary
C++11 Function Usage Scenarios