数组的输出

来源:互联网 发布:淘宝号申请免费注册 编辑:程序博客网 时间:2024/05/21 11:16
https://github.com/Mooophy/Cpp-Primer/blob/master/ch03/ex3_44.cpp
<textarea readonly="readonly" name="code" class="c++"> 
#include<iostream>
using namespace std; 
int main(int argc, char *argv[])
{
int ia[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
//for loop
for (size_t i = 0; i != 3; i++)
{
for (size_t j = 0; j != 4; j++)
cout << ia[i][j] << ' ';
cout << endl;
}
//range for 除了内层的循环外,其他所有的循环的控制变量应该都是引用型
for (auto &row : ia)//&引用
{
for (auto col : row)
cout << col<<' ';
cout <<endl;
}


for (int(&row)[4] : ia)
{
for (int col : row)
cout << col <<' ';
cout << endl;
}


//using pointers
for (int(*row)[4] = ia; row != ia + 3; row++)
{
for (int *col = *row; col != *row + 4; col++)
cout << *col << ' ';
cout << endl;
}
for (auto p = ia; p != ia + 3; p++)
{
for (auto q = *p; q != *p + 4; q++)
cout << *q << ' ';
cout << endl;
}
// a range for to manage the iteration
// use type alias
using int_array = int[4];
for (int_array& p : ia)
{
for (int q : p)
cout << q << ' ';
cout << endl;
}


return 0;

}




0 0