C + + Auto keyword

Source: Internet
Author: User

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

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.