首页 > 其他 > 详细

多维数组的输出方法

时间:2014-04-30 07:09:36      阅读:386      评论:0      收藏:0      [点我收藏+]
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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!