📜  如何在 C++ 中动态分配 3D 数组

📅  最后修改于: 2021-09-04 08:28:53             🧑  作者: Mango

先决条件:数组基础
在 C/C++ 中,多维数组用简单的话作为数组的数组。多维数组中的数据以表格形式(按行主顺序)存储。下面是声明N 维数组的一般形式:

多维数组的语法

3-D 数组是一个二维数组的数组

3D 数组的语法:

有关多维和 3D 数组的更多详细信息,请参阅 C++ 中的多维数组一文。

问题给定一个 3D 数组,任务是使用 C++ 中的 new 为 3D 数组动态分配内存。

解:在下面的方法中,使用的方法是制作两个二维数组,每个二维数组有 3 行 4 列,其值如下。

1 2 3 4
5 6 7 8
9 10 11 12

13 14 15 16
17 18 19 20
21 22 23 24

方法 1:使用单指针——在这种方法中,分配大小为 x*y*z的内存块,然后使用指针算法访问内存块。以下是相同的程序:

C++
// C++ program to dynamically allocate
// the memory for 3D array in C++
// using new operator
#include 
using namespace std;
 
// Driver Code
int main()
{
    // Dimensions of the 3D array
    int x = 2, y = 3, z = 4;
    int count = 0;
 
    // Allocate memory blocks
    // of size x*y*z
    int* a = new int[x * y * z];
 
    // Traverse the 3D array
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            for (int k = 0; k < z; k++) {
 
                // Assign values to the
                // memory blocks created
                *(a + i * y * z + j * z + k) = ++count;
            }
        }
    }
 
    // Traverse the 3D array again
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            for (int k = 0; k < z; k++) {
 
                // Print values of the
                // memory blocks created
                cout << *(a + i * y * z + j * z + k) << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
 
    // Deallocate memory
    delete[] a;
 
    return 0;
}


C++
// C++ program to dynamically allocate
// the memory for 3D array in C++
// using new operator
#include 
using namespace std;
 
// Driver Code
int main()
{
     
    // Dimensions of the 3D array
    int x = 2, y = 3, z = 4;
    int count = 0;
 
    // Allocate memory blocks of size
    // x i.e., no of 2D Arrays
    int*** a = new int**[x];
 
    for (int i = 0; i < x; i++) {
 
        // Allocate memory blocks for
        // rows of each 2D array
        a[i] = new int*[y];
 
        for (int j = 0; j < y; j++) {
 
            // Allocate memory blocks for
            // columns of each 2D array
            a[i][j] = new int[z];
        }
    }
 
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            for (int k = 0; k < z; k++) {
 
                // Assign values to the
                // memory blocks created
                a[i][j][k] = ++count;
            }
        }
    }
 
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            for (int k = 0; k < z; k++) {
 
                // Print values of the
                // memory blocks created
                cout << a[i][j][k] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
 
    // Deallocate memory
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            delete[] a[i][j];
        }
        delete[] a[i];
    }
    delete[] a;
 
    return 0;
}


输出:
1 2 3 4 
5 6 7 8 
9 10 11 12 

13 14 15 16 
17 18 19 20 
21 22 23 24

方法 2:使用三重指针 – 下图说明了这个概念:

以下是相同的程序:

C++

// C++ program to dynamically allocate
// the memory for 3D array in C++
// using new operator
#include 
using namespace std;
 
// Driver Code
int main()
{
     
    // Dimensions of the 3D array
    int x = 2, y = 3, z = 4;
    int count = 0;
 
    // Allocate memory blocks of size
    // x i.e., no of 2D Arrays
    int*** a = new int**[x];
 
    for (int i = 0; i < x; i++) {
 
        // Allocate memory blocks for
        // rows of each 2D array
        a[i] = new int*[y];
 
        for (int j = 0; j < y; j++) {
 
            // Allocate memory blocks for
            // columns of each 2D array
            a[i][j] = new int[z];
        }
    }
 
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            for (int k = 0; k < z; k++) {
 
                // Assign values to the
                // memory blocks created
                a[i][j][k] = ++count;
            }
        }
    }
 
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            for (int k = 0; k < z; k++) {
 
                // Print values of the
                // memory blocks created
                cout << a[i][j][k] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
 
    // Deallocate memory
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            delete[] a[i][j];
        }
        delete[] a[i];
    }
    delete[] a;
 
    return 0;
}
输出:
1 2 3 4 
5 6 7 8 
9 10 11 12 

13 14 15 16 
17 18 19 20 
21 22 23 24

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live