different types of initialization in 1.4a.string S ("ZHL"), int i (3); Bracket Initializationb.string s= "ZHL", int i= 3; equals Sign InitializationC.int Arr[4] = {1,2,3,4}, struct TM today ={0}; Pod data with curly bracesd.struct s {int x; s (): X (0) {}}; constructor member initialization;The class member of the pod array cannot be initialized in 2.c++03, nor can the initial pod array when using new[], C + + 11 is finally unified with curly braces;ps:pod:http://www.cnblogs.com/viscount/p/5842895.html3. For structural data, direct initialization;
1 classC2 {3 intA;4 intb;5 Public:6CintIintj);7 };8C C {0,0};//c++11 only. Equivalent to C C (0,0);9 int* A =New int[3] {1,2,0}; /c++ One onlyTen classX { One inta[4]; A Public: -X (): a{1,2,3,4} {}//c++11, initializing array members -};
4. For containers, do not use Push_back ()
1 //c++11 container initializer2Vector vs<string>={" First","Second","Third"};3Map singers =4{ {"Lady Gaga","+1 (212) 555-7890"},5{"Beyonce Knowles","+1 (212) 555-0987"}6};
5. The class can also be initialized directly;
1 class C 2 {3 int a=7//c++11only4public:5 C (); 6 };
6. Expansion
class int c;}; int Main () { new A; // c is uninitialized after new is finished; A new A (); // c is initialized after new is finished; }c++03 This strange problem, so that you sometimes use the very uncomfortable, so enjoy using c++11 it!
Initialization of C + + 11