8 C++

8.1 Initialization

  • 直接初始化
in a = 3 + 4
int a(3+4); //int a (3+4) is also okay, but you'd better remove the space
int a{3 + 4}; //int a {3 + 4}; is also okay
int a = {3 + 4}; // the same as above

int * i = new int(1);
double * d = new double{1.2f}

int a[] = {1, 3, 4};
int b[] {2, 4, 6};
vector<int> c{1, 3, 5};
map<int, float> d = { {1, 1.0f}, {2, 2.0f}, {5, 3.2f} };
  • 间接初始化
// construct a temp variable and copy that to `s`
string s=string("Hello")

8.2 tips

cout << typeid(1.2).name();