C + + Primer c++11 of Reading notes (ii)

Source: Internet
Author: User
Tags array definition function definition

1 new STL Template type,std::initializer_list<t>

C++11 adds the Initializer_list template type, which is used to provide parameter passing mechanism with variable lengths of the same type condition, and the header file is <initializer_list>.

Its specific interface can refer to Cplusplus.com's introduction, the address is as follows: Http://www.cplusplus.com/reference/initializer_list/initializer_list/?kw=initializer_list

Unlike vectors, the elements in the Initializer_list object are always constant values, and we cannot change the values of the elements in the Initializer_list object. its usage is as follows:
#include <initializer_list>//for testing, the write using refrence hereusingstd::cout;usingStd::endl;usingstd::initializer_list;//testing variable-length parametersvoidTestvariadicparams (initializer_list<int>iz) {cout<<"initializer_list Test"<<Endl;  for(ConstAuto &R:iz) cout<< R <<" "; cout<<Endl;}//Calling MethodsTestvariadicparams ({1,4,7});
2 new Function declaration form (back of function return type)

The code is as follows:

// function return type trailing return // The return value is a pointer to int[10] auto Func (intint(*) [ten]; // returns a pointer to Even[4] int even[] = {2,4,6,8* GETPTR (int

Provides a more flexible function definition and declaration mechanism.

3 New Keyword constexpr

C++11 provides a new keyword constexpr to qualify the function return value or the current expression to be a constant, and the compiler will statically check the constexpr-decorated function return value or variable at compile time. To some extent simplifying the array definition, the length needs to be specified as a const limit.

4 Proxy constructor Delegating constructor

To solve the problem of sharing initialization code among multiple different constructors, C++11 introduces a proxy constructor that enables the delegation of initialization code shared by multiple constructors to one or more constructors. The sample code is as follows:

// Delegating constructor class datype{public:    datype (intint  y): m_x (x), m_y (y) {}    Datype (int y): Datype (0, y) {}    private:     int m_x;     int m_y;};
5 conversion functions for string and number types

C++11 provides new conversion functions for string std::string and numeric types, which greatly reduces the pain of using C + + 's I/O mechanism and reduces the possibility of using the C function library. Unfortunately Mingw32 does not support to_string compilation, MINGW64 is said to support. Only sample code is shown here.

stringTo_string (intval);stringTo_string (Longval);stringTo_string (Long Longval);stringto_string (unsigned val);stringTo_string (unsignedLongval);stringTo_string (unsignedLong Longval);stringTo_string (floatval);stringTo_string (Doubleval);stringTo_string (Long DoubleVal);

function of string to numeric type:

Stoi//Convert string to Integer (function template)Stol//Convert string to long int (function template)Stoul//Convert string to unsigned integer (function template)Stoll//Convert string to long long (function template)Stoull//Convert string to unsigned long long (function template)Stof//Convert string to float (function template)Stod//Convert string to double (function template)Stold//Convert string to long double (function template)
6 provides a lambda expression and a new bind mechanism

Lambda expressions can be known as the syntax mechanism for anonymous, inline functions, and their specific syntax suggests reference to standard documents, or C++primer Ch 10.3 chapters.

The new bind function supports a more flexible parameter binding mechanism, which is much more useful than the bind1st and bind2nd of the previous STL internals.

Additional Instructions

This article is mainly about C + + Primer Fifth edition of the 5th chapter to the 12th chapter on the c++11 content of the collation.

All the code is compiled in the GCC v4.8.1 version of the test, the relevant source code can be downloaded from my git, the URL is as follows: Https://git.oschina.net/Tocy/SampleCode.git, located in the c++11 directory Cpp_ The Primer_test2.cpp file.

C + + Primer c++11 of Reading notes (ii)

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.