1. 程式人生 > 其它 >C++Primer 練習題3.43--3.45

C++Primer 練習題3.43--3.45

技術標籤:C++學習c++

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;
}