1.例题一:
#include<iostream>
using namespace std;
int main()
{
constexpr size_t row = 3, col = 4;
int ia[row][col];
unsigned cnt = 0;
for (auto &i : ia)
{
for (auto &j : i)
{
j = cnt;
cnt++;
}
}
for (auto &i : ia)
{
for (auto j : i)
cout << j << endl;
}
system("pause");
}
2.例题二:
#include<iostream>
#include<iterator>
using namespace std;
int ia[3][4];
int main()
{
for (auto p = begin(ia); p != end(ia); p++)
{
for (auto q = begin(*p); q != end(*p); q++)
{
cout << *q << ends;
}
}
system("pause");
}
3.练习3.43:
#include<iostream>
#include<iterator>
using namespace std;
int main()
{
int ia[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
//版本1:范围for语句
for (int(&i)[4] : ia)
{
for (int j : i)
cout << j << ends;
}
cout << endl;
//版本2:下标操作
for (size_t i = 0; i != 3; ++i)
{
for (size_t j = 0; j != 4; ++j)
cout << ia[i][j] << ends;
}
cout << endl;
//版本3:指针操作
for (int(*i)[4]=ia; i != ia + 3; ++i)
{
for (int *j = *i; j != *i + 4; ++j)
cout << *j << ends;
}
cout << endl;
system("pause");
}
4.练习3.44:
#include<iostream>
using namespace std;
int main()
{
int ia[3][4]{ 0,1,2,3,4,5,6,7,8,9,10,11 };
//类型别名
using int_array = int[4];
typedef int other;
for (int_array *p = ia; p != ia + 3; ++p)
{
for (other *q = *p; q != *p + 4; q++)
{
cout << *q << ends;
}
}
system("pause");
}
5.练习3.45:其实就是例题抄一遍。。
欢迎交流探讨。
谢谢。