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