C++遍歷二維數組的四種方法。本站提示廣大學習愛好者:(C++遍歷二維數組的四種方法)文章只能為提供參考,不一定能成為您想要的結果。以下是C++遍歷二維數組的四種方法正文
#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; }