1.統(tǒng)一的初始化方法
int arr[3]{ 1,2,3 };
vector<int> iv{ 1,2,3 };
map<int, string> mp{ {1,"a"} ,{1,"b"} };
string str{ "Hello World" };
int *p = new int[20]{ 1,2,3 };
//結(jié)構(gòu)體
struct A
{
int i, j;
A(int m, int n) :i(m),j(n){}
};
A func(int m, int n)
{
return{ m,n };
}
//
void main()
{
A *p = new A{ 3,7 };
}
2.成員變量默認(rèn)初始值
class B
{
public:
int m = 1234;
int n;
};
void main()
{
B b;
cout << b.m << endl; //1234
}
3.auto關(guān)鍵字
用于定義變量,編譯起可以自動判斷變量的類型
//auto關(guān)鍵字
auto i = 100; //i是int
auto p = new F(); //p是F *
auto k = 34343LL; //k是long long
map<string, int, greater<string>> mp;
//i的類型是:map<string,int,greater<string>>::iterator
for (auto i = mp.begin(); i != mp.end(); ++i)
{
cout << i->first << "," << i->second;
}
4.decltype關(guān)鍵字
求表達(dá)式的類型
int j;
double t;
struct A { double x; };
const A * a = new A();
decltype(a) x1; //x1 is A *
decltype(j) x2; //x2 is int
decltype(a->x) x3; //x3 is double
decltype((a->x)) x4 = t; //x4 is doubule &