New Keywords
Auto
The first effect of introducing auto in C++11 is for automatic type deduction.
Automatic type deduction for auto, used to infer the data type of a variable from an initialization expression. The automatic type deduction of auto can greatly simplify our programming work. Auto actually compiles the variables at compile time, so it does not adversely affect the running efficiency of the program. In addition, it seems that auto does not affect the compilation speed, because the compile time will also be the right derivation and then determine whether to match the left. If there is no auto keyword to write an iterator to write for a long time, this will save our brain cells.
Auto A;//error, Auto is derived by initializing the expression into the line type, if there is no initialization expression, it is not possible to determine the type of aAuto I=1; auto D=1.0; auto Str="Hello World"; auto Ch='A'; auto Func= less<int>(); Vector<int>IV; auto ITE=iv.begin (); auto P=NewFoo ()//yes? Custom type in-line type deduction
Auto not only has the above application, it is also a skill in the template, such as the following example of this process product example, if you do not use AUTO, you must declare the product template parameters:
void processproduct (const creator& Creator) {Product* val =//
If you use auto, you can write this:
void processproduct (const creator&=//
Discarding the problematic template parameters, the entire code becomes more positive.
Decltype
Decltype is actually a bit like auto's inverse function, Auto lets you declare a variable, and decltype can get the type from a variable or an expression, with the following example:
int 3 = x; //
Some people will ask, where is the utility of Decltype, and we continue with the above example, if we want to use the product as the return value in the example of the processed product above? We can write this:
Template <typename creator> auto processproduct (const creator& Creator)= //}
Nullptr
Nullptr is a new type introduced to solve the two semantic problem of NULL in the original C + + because NULL actually represents 0.
Sequence for loop
In C + +, A For loop can use a simplified for loop like Java, which can be used to iterate over arrays, containers, strings, and sequences defined by the Begin and end functions (that is, with iterator), as shown in the example code:
map<stringint> m{{"a"1}, {"b" 2}, {"C"3 for (auto p:m) {cout <<p.first<<""<<p.second<<
Lambda expression
Lambda expressions are similar to closures in JavaScript, which can be used to create and define anonymous function objects to simplify the programming effort. The syntax for Lambda is as follows:
[function Object parameter] (operator overloaded function parameter), return value type {function Body}
vector<int> iv{5,4,3,2,1}; intA =2, B =1; For_each (Iv.begin (), Iv.end (), [b] (int&X) {cout<< (x + b) <<endl;});//(1)For_each (Iv.begin (), Iv.end (), [=] (int&x) {x *= (a + b);});//(2)For_each (Iv.begin (), Iv.end (), [=] (int&X)int{returnX * (A + b);});//(3)
The parameters within [] refer to the global variables that a lambda expression can take. (1) B in the function means that the function can get a global variable outside the lambda expression, and if it is passed in [], that is, all external variables can be obtained, such as (2) and (3) the parameters in the lambda expression () are the arguments passed in each time the function is called.
This is followed by the type of the lambda expression return value, such as a variable of type int returned in (3).
Template for variable length parameters
We've used Pair,pair in C + + to construct a container that contains two different types of data using the Make_pair construct. For example, the following code:
Auto P = make_pair ("C + +
Since the variable-length parameter template was introduced in c++11, a new data type was invented: Tuple,tuple is an n-tuple that can pass in 1, 2 or more different types of data.
Auto T1 = make_tuple (12.0"C + +one"= make_tuple (1 2.0"C + +one", {102
This avoids the ugly practice of nesting pair in the previous pair, which makes the code cleaner.
Another common example is the print function, where printf can pass in multiple parameters in C, and in c++11, we can use the variable-length parameter template to achieve a more concise print:
void Print (head Head, TypeName ... tail) {cout<< head <<
Several different kinds of parameters can be passed in print, as follows:
Print (11.0"c++11"
A more elegant initialization method
Before introducing c++11, only arrays can use the initialization list, and other containers want to use the initialization list, only in the following ways:
int arr[3] = {123} vector<int3
In c++11, we can use the following syntax to replace:
intarr[3]{1,2,3}; Vector<int> iv{1,2,3}; Map<int,string>{{1,"a"}, {2,"b"}}; stringstr{"Hello World"};
In addition, the smart pointer is also very useful, a code write data type hundreds of characters. But vs2012 does not support c++11, so use C++11 's advice to install vs2013.
C++11 new Features