#include<iostream>
using namespace std;
int main(){
int arr[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
//练习3.43
//版本1:范围for
for (const int (&i)[4] : arr) {
for (int j : i) {
cout << j << ' ';
}
}
cout << endl;
//版本2:普通for,下标运算符
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << arr[i][j] << ' ';
}
}
cout << endl;
//版本3:指针
for (int (*i)[4] = begin(arr); i < end(arr); i++) {
for (int *j = begin(*i); j < end(*i); j++) {
cout << *j << ' ';
}
}
cout << endl;
//练习3.44:类型别名
//版本1
using int_array = int[4];
for (const int_array &i : arr) {
for (int j : i) {
cout << j << ' ';
}
}
cout << endl;
//版本2同上
//版本3
for (int_array *i = begin(arr); i < end(arr); i++) {
for (int *j = begin(*i); j < end(*i); j++) {
cout << *j << ' ';
}
}
cout << endl;
//练习3.45:auto
//1
for (auto &i : arr) {
for (auto j : i) {
cout << j << ' ';
}
}
cout << endl;
//2:同上,没必要用auto
//3
for (auto i = begin(arr); i < end(arr); i++) {
for (auto j = begin(*i); j < end(*i); j++) {
cout << *j << ' ';
}
}
cout << endl;
return 0;
}
(书本练习题比较简单,但做一做总会有收获)