1, the advantages of auto key one
The Auto keyword knows that the compiler pushes the type of the variable based on the initial expression of the variable, with the advantages of using auto mainly:
(1) Robustness: When the expression type changes, including the function return value type transformation, the program can still run correctly, do not need to change all the variable type.
(2) Performance: type conversion can be eliminated.
(3) Ease of use: Don't worry about the type name error
(4) Efficient: will make the program more effective rate
Auto is a type placeholder and is not a type in itself, so you cannot use sizeof or typeid
2. The Auto keyword will remove the variable's reference,const,volite volatile modifier
//Cl.exe/analyze/ehsc/w4#include <iostream>using namespacestd;intMain () {intCount =Ten; int& countref =count; Auto Myauto=Countref; Countref= One; cout<< Count <<" "; Myauto= A; cout<< Count <<Endl;}
In the above code, Myauto is an int, not an int & The result of the output is 11 11, not 11 12.
3. Auto Use instance
1, the following two declarations are equivalent, using Auto is more convenient.
int 0; // Variable J is explicitly type int. 0 // Variable k is implicitly type int because 0 are an integer.
map<int,list<string>>::iterator i =
2, for the For loop, use auto more concise
//Cl/ehsc/nologo/w4#include <deque>using namespacestd;intMain () {deque<Double> Dqdoubledata (Ten,0.1); for(Auto iter = Dqdoubledata.begin (); ITER! = Dqdoubledata.end (); + +ITER) { /* ... */ } //prefer range-for loops with the following information in mind//(this applies to any range-for with auto, not just deque) for(Auto Elem:dqdoubledata)//COPIES elements, not much better than the previous examples{/* ... */ } for(auto& Elem:dqdoubledata)//observes and/or modifies elements in-place{/* ... */ } for(Constauto& elem:dqdoubledata)//observes elements In-place{/* ... */ }}
3. Auto can be used for new and pointers
Double 12.34 Newnew Auto (&x);
4. Multiple variables can be declared in a declaration statement
1 // resolves to int. Auto A (2.01), *b (&a); // resolves to double. ' a ', *d (&c); // resolves to char. 1, &n = m; // resolves to int.
5. Auto can be used for conditional operators
int - $ = v1 > v2? V1:v2;
6. In the code below, X is int,y as a const int &,FP as a function pointer
int f (intreturn x;} int Main () { = f (0); Const Auto & y = f (1); int (*p) (int x); = F; = p; // ...}
C + + Auto keyword