C++11 Function Usage Scenarios

Source: Internet
Author: User

"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

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.