1.使用auto关键字
#include
<iostream>
using
namespace std;
int
main()
{
int a[3][4]={
{0,1,2,3},
{4,5,6,7},
{8,9,0,1}
};
cout<<"method 1:"<<endl;
for(auto &i:a){
for(int j:i)
cout<<j<<" ";
cout<<endl;
}
cout<<endl<<endl<<"method
2:"<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<4;j++)
cout<<a[i][j]<<"
";
cout<<endl;
}
cout<<endl<<endl<<"method
3:"<<endl;
for(auto *p=a;p!=a+3;++p){
for(auto q=*p;q!=*p+4;++q)
cout<<*q<<" ";
cout<<endl;
}
}
2.不使用类型别名,不使用auto关键字,不使用decltype关键字:
#include
<iostream>
using
namespace std;
int
main()
{
int a[3][4]={
{0,1,2,3},
{4,5,6,7},
{8,9,0,1}
};
cout<<"method 1:"<<endl;
for(int (&i)[4]:a){
for(int j:i)
cout<<j<<" ";
cout<<endl;
}
cout<<endl<<endl<<"method
2:"<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<4;j++)
cout<<a[i][j]<<"
";
cout<<endl;
}
cout<<endl<<endl<<"method
3:"<<endl;
for(int (*p)[4]=a;p!=a+3;++p){
for(int *q=*p;q!=*p+4;++q)
cout<<*q<<" ";
cout<<endl;
}
}
3.使用类型别名
#include
<iostream>
using
namespace std;
using
int_array=int[4];
int
main()
{
int a[3][4]={
{0,1,2,3},
{4,5,6,7},
{8,9,0,1}
};
cout<<"method 1:"<<endl;
for(int_array &i:a){
for(int j:i)
cout<<j<<" ";
cout<<endl;
}
cout<<endl<<endl<<"method
2:"<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<4;j++)
cout<<a[i][j]<<"
";
cout<<endl;
}
cout<<endl<<endl<<"method
3:"<<endl;
for(int_array *p=a;p!=a+3;++p){
for(int *q=*p;q!=*p+4;++q)
cout<<*q<<" ";
cout<<endl;
}
}
多维数组的输出方法,布布扣,bubuko.com
多维数组的输出方法
原文:http://www.cnblogs.com/wanglingtianshi/p/3690183.html