[問題] 指標與多維Arrays
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VS2008
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
利用指標將三維矩陣輸出
餵入的資料(Input):
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
初學C++對指標很陌生,不太懂要如何製造多維Arrays
請幫我看下應該怎樣修改比較好~~
程式碼(Code):(請善用置底文網頁, 記得排版)
http://ideone.com/Y5DmA
#include<iostream>
#include<string>
#include<cstring>
#include<cctype>
#include<vector>
using namespace std;
int main()
{
const int row=3,col=4,page=2;
int mat[row][col][page]; //mat[i][j][k]
for(size_t i=0;i!=row;i++){
for(size_t j=0;j!=col;j++){
for(size_t k=0;k!=page;k++){
mat[i][j][k]=5*(k+1)*(i+1);
}
}
}
for(size_t k=0;k!=2;k++){
cout<<"**********第一維
mat[i][j]["<<k+1<<"]*******"<<endl;
for(size_t i=0;i!=3;i++){
for(size_t j=0;j!=4;j++){
cout<<mat[i][j][k]<<"\t";
}
cout<<endl;
}
cout<<"\n\n\n*********************************"<<endl;
}
//int (*ip)[4][2]=mat;
typedef int onedim[page];
typedef onedim twodim[col];
twodim *ip=mat;
for(twodim *p=mat;p!=mat+page;p++){
for(onedim *q=*p;q!=p+col;p++){
for(int *r=**p;r!=r+row;r++){
cout<<***r<<"\t";
}
cout<<endl;
}
cout<<"*******下一個矩陣******************"<<endl;
}
return 0;
}
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.184.201
※ 編輯: Ares0628 來自: 140.113.184.201 (03/01 17:35)
※ 編輯: Ares0628 來自: 140.113.184.201 (03/01 17:36)
※ 編輯: Ares0628 來自: 140.113.184.201 (03/01 17:38)
→
03/01 20:14, , 1F
03/01 20:14, 1F