2010-10-11 75 views
13

我是新的C/C++,我一直在破解我的頭,但仍然有不知道如何做一個「結構」這樣的3D陣列C++使用INT []操作

alt text

它應該是一個使用指針的3D動態數組。

我開始喜歡這個,但卡住了有

int x=5,y=4,z=3; 
    int ***sec=new int **[x]; 

這將是足以知道如何使它爲y和z靜態大小;

請,我會很感激你幫助我。

在此先感謝。

回答

19

要創建動態的3D整數數組,首先要理解1D和2D數組。

一維數組:您可以通過

const int MAX_SIZE=128; 
int *arr1D = new int[MAX_SIZE]; 

這裏做到這一點很容易,我們正在創造一個int指針將指向的內存塊,其中整數可以存儲。

二維數組:您可以使用上述一維數組的解決方案來創建二維數組。首先,創建一個指針,該指針應該指向一個內存塊,其中只保留其他指向實際數據的整數指針。由於我們的第一個指針指向一個指針數組,因此這將被稱爲指針指針(雙指針)。

const int HEIGHT=20; 
const int WIDTH=20; 

int **arr2D = new int*[WIDTH]; //create an array of int pointers (int*), that will point to 
           //data as described in 1D array. 
for(int i = 0;i < WIDTH; i++){ 
     arr2D[i] = new int[HEIGHT]; 
} 

3D陣列:這是你想要做什麼。在這裏你可以嘗試兩種情況下使用的方案。應用與2D陣列相同的邏輯。有問題的圖解釋了所有。第一個數組是指向指針的指針(int *** - 因爲它指向了雙指針)。解決方案如下:

const int X=20; 
const int Y=20; 
const int z=20; 

int ***arr3D = new int**[X]; 
for(int i =0; i<X; i++){ 
    arr3D[i] = new int*[Y]; 
    for(int j =0; j<Y; j++){ 
     arr3D[i][j] = new int[Z]; 
     for(int k = 0; k<Z;k++){ 
      arr3D[i][j][k] = 0; 
     } 
    } 
} 
+0

此外,您可以使用此方法用於具有N-1 for循環的N維數組。 – 2010-10-11 08:43:20

+7

也許會提及相應的刪除功能。 – stijn 2010-10-11 08:47:26

+0

@Manish Shukla在3D Array代碼中,X,Y和Z中的哪一個表示寬度,高度和深度? – Mariya 2014-04-16 16:40:44

0

OK,讓我們把你的起點

int ***sec = new int**[x]; 

秒是現在長度x的INT **組成的數組,所以現在我只想專注於使第0個元素是你想要的

sec[0] = new int*[y]; 

現在秒[0]指向長度Y的INT * S的陣列,現在只需要來完成樹的最後一位,所以

sec[0][0] = new int[z]; 

最後得到它的表格圖中

sec[0][0][z-1] = 0; 

這似乎有點像一門功課的問題,確保你真正瞭解的答案,爲什麼它的工作原理。

+0

'sec'不是一個數組。它是一個指向數組的第一個元素的指針。 – sellibitze 2010-10-11 09:33:44

+0

這是相當語義的。在int x [10]等情況下要公平; x實際上是指向數組的第一個元素的指針和數組。作爲指向數組的第一個元素的指針和作爲數組之間確實沒有區別。 – 2010-10-11 17:50:28

1

你可以試試:

for(int i=0;i<x;i++) { 
    sec[i] = new int *[y]; 
    for(int j=0;j<y;j++) { 
    sec[i][j] = new int [z]; 
    } 
} 

一旦您使用此內存中完成,你可以釋放它爲:

for(int i=0;i<x;i++) { 
    for(int j=0;j<y;j++) { 
    delete [] sec[i][j]; 
    } 
    delete [] sec[i]; 
} 
delete [] sec; 
11
// one-liner 
typedef std::vector<std::vector<std::vector<int> > > ThreeDimensions; 
// expanded 
typedef std::vector<int> OneDimension; 
typedef std::vector<OneDimension> TwoDimensions; 
typedef std::vector<TwoDimension> ThreeDimensions; 

(這是標記C++,畢竟)

編輯迴應Joe的問題

你好再喬=)當然。這裏的例子:

#include <vector> 
#include <iostream> 

int main(int argc, char* const argv[]) { 

    /* one-liner */ 
    typedef std::vector<std::vector<std::vector<int> > >ThreeDimensions; 
    /* expanded */ 
    typedef std::vector<int>OneDimension; 
    typedef std::vector<OneDimension>TwoDimensions; 
    typedef std::vector<TwoDimensions>ThreeDimensions; 

    /* 
     create 3 * 10 * 25 array filled with '12' 
    */ 
    const size_t NElements1(25); 
    const size_t NElements2(10); 
    const size_t NElements3(3); 
    const int InitialValueForAllEntries(12); 

    ThreeDimensions three_dim(NElements3, TwoDimensions(NElements2, OneDimension(NElements1, InitialValueForAllEntries))); 

    /* the easiest way to assign a value is to use the subscript operator */ 
    three_dim[0][0][0] = 11; 
    /* now read the value: */ 
    std::cout << "It should be 11: " << three_dim[0][0][0] << "\n"; 
    /* every other value should be 12: */ 
    std::cout << "It should be 12: " << three_dim[0][1][0] << "\n"; 

    /* get a reference to a 2d vector: */ 
    TwoDimensions& two_dim(three_dim[1]); 

    /* assignment */ 
    two_dim[2][4] = -1; 
    /* read it: */ 
    std::cout << "It should be -1: " << two_dim[2][4] << "\n"; 

    /* get a reference to a 1d vector: */ 
    OneDimension& one_dim(two_dim[2]); 

    /* read it (this is two_dim[2][4], aka three_dim[1][2][4]): */ 
    std::cout << "It should be -1: " << one_dim[4] << "\n"; 
    /* you can also use at(size_t): */ 
    std::cout << "It should be 12: " << one_dim.at(5) << "\n"; 

    return 0; 
} 
+0

+1要走的路*如果*這個內存佈局真的是OP想要的 – sellibitze 2010-10-11 08:47:58

1

全面的答案。

如果你真的用C++編寫(不粗略的C),我想你應該再看看這個複雜的數據結構。國際海事組織重新設計,同時牢記你想要做的事情會更好。

1

你想做的事情在C++中不是慣用的。當然,你可以爲此使用int***pointer,但這是非常不鼓勵。在C++中,我們有更好的方法到達那裏。

vector<vector<vector<int> > > foo (5,vector<vector<int> >(4, vector<int>(3))); 

這將導致內存佈局與您要求的相似。它支持動態調整大小和內部向量,使其具有不同的大小,就像在圖片中一樣。另外,您不必擔心手動分配/刪除任何內容。另外,矢量知道它們的大小,所以你不必在某個地方記住它。

但是,如果您只想要一個「矩形」3D數組,其中所有元素連續存儲在同一個內存塊中,則可以使用boost::multiarray