如何使用new运算符在C++中动态声明二维数组

如何使用new运算符在C++中动态声明二维数组

在 C/C++ 中,多维数组可以简单理解为数组的数组。多维数组中的数据以表格形式(按行主序)存储。下面是声明 N 维数组的一般形式:

N维数组的语法:

data_type array_name[size1][size2]….[sizeN];

data_type: 要存储在数组中的数据类型。

在这里,data_type 是有效的 C/C++数据类型。

array_name: 数组的名称

size1、size2、…、sizeN: 维度的大小

2D 数组是一维数组的数组。

2D 数组的语法:

data_type array_name[x][y];

data_type: 要存储的数据类型。有效的 C/C++ 数据类型。

下面是二维数组的图示表示:

如何使用new运算符在C++中动态声明二维数组

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

问题: 给定一个 2D 数组,任务是在 C++ 中使用 new 动态分配二维数组的内存。

解决方案: 使用以下值声明一个 3 行 4 列的 2D 数组:

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

注意: 这里 M 是行数, N 是列数。

方法一: 使用单个指针——在此方法中,分配了大小为 M*N 的内存块,然后使用指针算术访问内存块。下面是相应程序:

// C++ program to dynamically allocate
// the memory for 2D array in C++
// using new operator
#include <iostream>
using namespace std;

// Driver Code
int main()
{
    // Dimensions of the 2D array
    int m = 3, n = 4, c = 0;

    // Declare a memory block of
    // size m*n
    int* arr = new int[m * n];

    // Traverse the 2D array
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {

            // Assign values to
            // the memory block
            *(arr + i * n + j) = ++c;
        }
    }

    // Traverse the 2D array
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {

            // Print values of the
            // memory block
            cout << *(arr + i * n + j)
                  << " ";
        }
        cout << endl;
    }

      //Delete the array created
      delete[] arr;

    return 0;
}  

输出:

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

方法二: 使用指针数组:这里创建了一个指针数组,然后对每个内存块进行操作。下面是说明该概念的图示:

如何使用new运算符在C++中动态声明二维数组

下面是相应的程序:

// C++ program to dynamically allocate
// the memory for 2D array in C++
// using an array of pointers
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Dimensions of the 2D array
    int m = 3, n = 4, c = 0;
 
    // Create an array of pointers
    // dynamically
    int** arr = new int*[m];
 
    // Traverse through each pointer
    for (int i = 0; i < m; i++) {
        // Allocate memory of size n with
        // each pointer of array
        arr[i] = new int[n];
    }
    // Assign values to allocated memory
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            arr[i][j] = ++c;
        }
    }
   
    // Traverse through the array
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cout << arr[i][j]
                 << " ";
        }
        cout << endl;
    }
 
    // Delete the array of pointers
    for (int i = 0; i < m; i++) {
        delete[] arr[i];
    }
    delete[] arr;
 
    return 0;
}

输出:

1 2 3 4 
5 6 7 8 
9 10 11 12
// C++程序:在C++中使用new运算符
// 动态分配二维数组的内存
#include <iostream>
using namespace std;

// 主函数
int main()
{
    // 数组的维度
    int m = 3, n = 4, c = 0;

    // 声明大小为M的内存块
    int** a = new int*[m];

    for (int i = 0; i < m; i++) {

        // 声明大小为n的内存块
        a[i] = new int[n];
    }

    // 遍历二维数组
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {

            // 为创建的内存块赋值
            a[i][j] = ++c;
        }
    }

    // 遍历二维数组
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {

            // 输出创建的内存块的值
            cout << a[i][j] << " ";
        }
        cout << endl;
    }

    // 删除已创建的数组
    for (int i = 0; i < m; i++)
        // 删除内部数组
        delete[] a[i];
    delete[] a; // 删除包含所有内部数组指针的外部数组

    return 0;
}  

输出:

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程