C++Primer 練習題3.43--3.45
阿新 • • 發佈:2020-12-30
3.43
版本一:
#include<iostream>
using namespace std;
int ia[3][4]={1,3,2,4,5,11,9,7,8,10,6,12};
int main()
{
for(const int (&row) [4] : ia)
for(const int col : row)
cout<<col<<" ";
return 0;
}
版本二:
#include<iostream>
using namespace std;
int ia[3][4]={1,3,2,4,5,11,9,7,8,10,6,12};
int main()
{
for(int i=0;i<3;i++)
for(int j=0;j<4;j++)
cout<<ia[i][j]<<" ";
return 0;
}
版本三:
#include<iostream>
using namespace std;
int ia[3][4]={1,3,2,4,5,11,9,7,8,10,6,12};
int main()
{
for(int(*p) [4] = begin (ia);p!=end(ia);p++)
for(int* q= begin(*p);q!=end(*p);q++)
cout<<*q<<" ";
return 0;
}
3.44
#include <iostream>
using namespace std;
using int_array = int [4];
//typedef int int_array [4];//兩種寫法我推薦使用的一種
int ia[3][4] = {1, 3, 2, 4, 5, 11, 9, 7, 8, 10, 6, 12};
int main ()
{
for (int_array *p = begin(ia); p != end(ia); p++)
for (int *q = begin(*p); q != end(*p); q++)
cout << *q << " ";
return 0;
}
3.45
#include <iostream>
using namespace std;
int ia[3][4] = {1, 3, 2, 4, 5, 11, 9, 7, 8, 10, 6, 12};
int main()
{
for (auto &row : ia)
for (auto &col : row)
cout << col <<" ";
return 0;
}