数组名是 decltype
关键字的参数
int a[5] = {1,2,3,4,5};
decltype(a) b = {2,4,5,6,7}; // b是有5个整型变量的数组
数组名是&
、sizeof
、typeid
运算符的运算对象
// &
int (*pa)[5] = &a;
for (auto n : *a)
cout << n << ' ';
cout << endl;
// sizeof
cout << sizeof(a) << endl;
// typeid
cout << typeid(a).name() << endl;
1 2 3 4 5
20
A5_i
给数组绑定引用
int (&r)[5] = a;
for (auto n : r)
cout << n << ' ';
cout << endl;
1 2 3 4 5
如果要用范围
for
语句处理多维数组,除了最内层循环外,其他所有循环控制变量都应该是引用类型;否则就会自动转首元素指针,报错。
int a[4][5][2] = {5,3,2,1,4,6,7};
for (auto &n1 : a)
for (auto &n2 : n1)
for (auto n3 : n2) // 最内层可引用可不引用,当要改变元素值则必须引用
cout << n3 << endl;