1.统一的初始化方法
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 };
//结构体
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.成员变量默认初始值
class B
{
public:
int m = 1234;
int n;
};
void main()
{
B b;
cout << b.m << endl; //1234
}
3.auto关键字
用于定义变量,编译起可以自动判断变量的类型
//auto关键字
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关键字
求表达式的类型
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 &