C++ 多维数组

在 C++ 中,我们可以创建一个数组的数组,称为多维数组。例如

int x[3][4];

在这里,x 是一个二维数组。它可以容纳最多 12 个元素。

我们可以将此数组看作一个包含 3 行的表,每行有 4 列,如下所示。

C++ two dimensional array
C++ 编程中二维数组的元素

三维数组也以类似的方式工作。例如

float x[2][4][3];

该数组 x 最多可以容纳 24 个元素。

我们可以通过将维数相乘来找出数组中的总元素数

2 x 4 x 3 = 24

多维数组初始化

与普通数组一样,我们可以通过多种方式初始化多维数组。

1. 二维数组的初始化

int test[2][3] = {2, 4, 5, 9, 0, 19};

不推荐使用上述方法。以下是使用相同数组元素初始化此数组的更好方法

int  test[2][3] = { {2, 4, 5}, {9, 0, 19}};

该数组有 2 行 3 列,因此我们有两行元素,每行有 3 个元素。

C++ Two-dimensional array initialization
在 C++ 中初始化二维数组

2. 三维数组的初始化

int test[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23, 
                 2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};

这不是初始化三维数组的好方法。初始化此数组的更好方法是

int test[2][3][4] = { 
                     { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
                     { {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} }
                 };

请注意此三维数组的维数。

第一个维数的值为 2。因此,构成第一个维度的两个元素是

Element 1 = { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} }
Element 2 = { {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} }

第二个维数的值为 3。请注意,第一个维度的每个元素都有三个元素

{3, 4, 2, 3}, {0, -3, 9, 11} and {23, 12, 23, 2} for Element 1.
{13, 4, 56, 3}, {5, 9, 3, 5} and {5, 1, 4, 9} for Element 2.

最后,在第二个维度的每个元素中都有四个 int 数字

{3, 4, 2, 3}
{0, -3, 9, 11}
... .. ...
... .. ...

示例 1:二维数组

// C++ Program to display all elements
// of an initialised two dimensional array

#include <iostream>
using namespace std;

int main() {
    int test[3][2] = {{2, -5},
                      {4, 0},
                      {9, 1}};

    // use of nested for loop
    // access rows of the array
    for (int i = 0; i < 3; ++i) {

        // access columns of the array
        for (int j = 0; j < 2; ++j) {
            cout << "test[" << i << "][" << j << "] = " << test[i][j] << endl;
        }
    }

    return 0;
}

输出

test[0][0] = 2
test[0][1] = -5
test[1][0] = 4
test[1][1] = 0
test[2][0] = 9
test[2][1] = 1

在上面的示例中,我们初始化了一个名为 test 的二维 int 数组,它有 3 行和 2 列。

在这里,我们使用嵌套的 for 循环来显示数组元素。

  • i == 0i == 2 的外部循环访问数组的行
  • j == 0j == 1 的内部循环访问数组的列

最后,我们在每次迭代中打印数组元素。


示例 2:为二维数组输入数据

#include <iostream>
using namespace std;

int main() {
    int numbers[2][3];

    cout << "Enter 6 numbers: " << endl;

    // Storing user input in the array
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            cin >> numbers[i][j];
        }
    }

    cout << "The numbers are: " << endl;

    //  Printing array elements
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << "numbers[" << i << "][" << j << "]: " << numbers[i][j] << endl;
        }
    }

    return 0;
}

输出

Enter 6 numbers: 
1
2
3
4
5
6
The numbers are:
numbers[0][0]: 1
numbers[0][1]: 2
numbers[0][2]: 3
numbers[1][0]: 4
numbers[1][1]: 5
numbers[1][2]: 6

在这里,我们使用 嵌套 for 循环 来输入 2d 数组。输入完成后,我们使用另一个嵌套的 for 循环来打印数组成员。


示例 3:三维数组

// C++ Program to Store value entered by user in
// three dimensional array and display it.

#include <iostream>
using namespace std;

int main() {
    // This array can store upto 12 elements (2x3x2)
    int test[2][3][2] = {
                            {
                                {1, 2},
                                {3, 4},
                                {5, 6}
                            }, 
                            {
                                {7, 8}, 
                                {9, 10}, 
                                {11, 12}
                            }
                        };

    // Displaying the values with proper index.
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 2; ++k) {
                cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k] << endl;
            }
        }
    }

    return 0;
}

输出

test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12

打印 3d 数组元素的基​​本概念与 2d 数组类似。

但是,由于我们正在处理 3 个维度,因此我们使用嵌套 for 循环,总共 3 个循环,而不是仅仅 2 个

  • i == 0i == 1 的外部循环访问数组的第一个维度
  • j == 0j == 2 的中间循环访问数组的第二个维度
  • k == 0k == 1 的最内层循环访问数组的第三个维度

正如我们所见,随着维数的增加,数组的复杂性呈指数级增长。


另请阅读

在结束之前,让我们来测试一下您对 C++ 多维数组的掌握程度!您能解决以下挑战吗?

挑战

编写一个函数,将元素添加到嵌套数组中。

  • 将一个整数添加到每个子数组的末尾。
  • 返回更新后的嵌套数组。
  • 例如,如果 arr[] = {{1,2},{3,4},{5,6}}num = 7,则预期输出为 {{1,2,7}, {3,4,7}, {5,6,7}}
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战