"C++primer" Review--with c++11 [2]

Source: Internet
Author: User

1. Array reference parameters, C + + allows variables to be defined as a reference to the array, given the same reason, the formal parameter can also be a reference to the arrays, when the reference parameter is bound to the corresponding argument, that is, bound to the array

1 void print (int (&arr) [ten])2{3for     (auto Elem:arr)4         cout << elem << Endl;   5 }

The

contains a function with deformable parameters, and C++11 provides two main methods: if all arguments are of the same type, you can pass a standard library type named Initializer_list, or, if the arguments are of different types, you can write a special function, the so-called mutable parameter template. Initializer_list is a standard library type that is used to represent a particular type of array, which is defined in a header file of the same name, and as a vector, it is also a template type, when defining an object, you must describe the type of elements contained in the list, and unlike vectors, initializer_li9st the element in the play is always a constant value and cannot change the value of the element in the object.

1 void err_msg (initializer_list<string> il)2{3     for (Auto Beg = Il.begin (); Beg! = Il.end (); + +Beg) 4        " " ; 5    cout << Endl; 6 }

If you want to pass a worthwhile sequence to a initializer_list parameter, you must place the sequence in a pair of curly braces;

1 if (Expected! = Actual) 2    Error_msg ({"functionx", "expected, actual}"); 3 Else 4    Error_msg ({"functionx""okay"});

The other method is to omit the parameter, which is set for C + + programs to access some special C code, which uses the C standard library function named VarArgs. Typically, an ellipsis parameter is not applied to other purposes.

2. Functions can return curly braces to defend the worthy list, similar to other return results, where the list page is used to initialize the temporary two that represents the function return, if the list is empty, the temporary two-knowledge initialization; otherwise, the returned value has the function's return type determined, in the following example, returns a vector formation, Use him to store information.

1vector<string>process ()2{if(expected.empty))3       return {};4    Else if(expencted = =Actual)5       return{"Functionx","OK"};6    Else 7       return{"Functionx", expected, actual};8}
1 int arr[]; 2 int *p1[]; 3 int (*P2) [P2] is a pointer to an array of 10 integers

3.assert preprocessing macro is actually a preprocessing variable that behaves like an inline function, and the Assert macro uses an expression as his condition: assert (expr); First, if the expression is false, Assert output information and abort the program's knowledge, if true, then assert does nothing. Assert is used to check for "cannot occur" condition

1 assert (Word.size () > Threshold);

。 When the assert does not need to function in the run, it can be canceled by defining the preprocessing variables of the ndebug, or by giving the compiler option D or-D. It is equivalent to writing a # define Ndebug in the first line of the main file

"C++primer" Review--with c++11 [2]

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.