C + + Programming (4th edition) Reading notes _ basic knowledge

Source: Internet
Author: User

Assigning values to variables

The commonly used variable assignment is to use "=" to assign values.

1 int 2;

However, if you assign a floating-point number to I, it will result in a loss of precision, preferably in C + + using the initialization list of the way "{}" to assign a value to the variable, so as to ensure that some types of information loss may cause the conversion of the type

1 #include <iostream>2usingnamespace  std; 3 4 int Main () {5     int I {2.3}; 6     return 0 ; 7 }

For example, the compiler will make an error.

<source>: in function ' int main () ': 5: <source>:5:12:error:narrowing conversion of ' 2.2999999999999998e+0 ' F Rom ' double ' to ' int. ' inside {} [-wnarrowing]int I {2.3};^compiler exited with result code 1 AutoLearn how to use auto with a for loop
1#include <iostream>2 using namespacestd;3 4 intMain () {5     intV[] = {0,1,2,3,4};6 7      for(Auto x:v) {8cout << x <<"\ t";9     }Tencout <<Endl; One      A      for(Auto i =0; I <sizeof(v)/sizeof(int); ++i) { -cout << V[i] <<"\ t";  -     } thecout <<Endl; -      -      for(Auto x: {0,1,2,3,4}) { -cout << x <<"\ t"; +     } -cout <<Endl; +      A      for(Auto x:v) { atX + =1; -     } -      for(Auto x:v) { -cout << x <<"\ t"; -     } -cout <<Endl; in      -     /*for cases without references, it can be understood that each element of V is put into X from beginning to end and printed*/ to      for(Auto &x:v) { +X + =1; -     } the      for(Auto x:v) { *cout << x <<"\ t"; $     }Panax Notoginsengcout <<Endl; -      the     return 0; +}

C + + Programming (4th edition) Reading notes _ basic knowledge

Related Article

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.