首页 > 编程语言 > 详细

C++遍历二维数组的四种方法

时间:2017-03-01 23:02:28      阅读:612      评论:0      收藏:0      [点我收藏+]
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::begin;
using std::end;
int main()
{
int ia[3][4];
size_t cnt=0;
for (auto &row:ia){
    for (auto &col:row){
        col=cnt;
        ++cnt;
    }
}

constexpr size_t rowCnt=3,colCnt=4;
for (size_t i=0;i != rowCnt; ++i){
    for (size_t j=0;j != colCnt;++j){
        cout << ia[i][j] <<endl;
    }
}
for (int(*p)[4] = ia;p != ia+3;p++){
    for (int *q = *p;q != *p+4;q++){
        cout << *q << endl;
    }
}
for (int(*p)[4] = begin(ia);p != end(ia);p++){
    for (int *q = begin(*p);q != end(*p); q++){
        cout << *q <<endl;
    }
}
for (int (&p)[4]:ia){
    for (int &q:p){
        cout << q << endl;
    }
}
return 0;
}

 

C++遍历二维数组的四种方法

原文:http://www.cnblogs.com/ywliao/p/6486477.html

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