C/C++中如何使用多维数组?解析和用法示例

2021年3月31日17:51:18 发表评论 748 次浏览

数组基础

在C/C++中, 我们可以将简单单词中的多维数组定义为数组数组。多维数组中的数据以表格形式(以行主要顺序)存储。

声明N维数组的一般形式:

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

data_type: Type of data to be stored in the array. 
           Here data_type is valid C/C++ data type
array_name: Name of the array
size1, size2, ... , sizeN: Sizes of the dimensions

例子:

Two dimensional array:
int two_d[10][20];

Three dimensional array:
int three_d[10][20][30];

多维数组的大小

可以通过将所有维的大小相乘来计算可以存储在多维数组中的元素总数。

例如:

数组int x[10][20]可以存储total(10*20) = 200个元素。

类似地,数组int x[5][10][20]可以存储total(5*10*20) = 1000个元素。

二维数组

二维数组是多维数组的最简单形式。我们可以将二维数组视为一维数组的数组, 以便于理解。

声明大小为x, y的二维数组的基本形式:

语法如下:

data_type array_name[x][y];
data_type: Type of data to be stored. Valid C/C++ data type.

我们可以将尺寸为10, 20的二维整数数组'x'声明为:int x [10] [20];

二维数组中的元素通常用x [i] [j]表示, 其中i是行号, " j"是列号。

二维数组可以看作是具有" x"行和" y"列的表, 其中行号的范围是0到(x-1), 列号的范围是0到(y-1)。下图显示了具有3行3列的二维数组" x":

二维

初始化二维数组:可以通过两种方式初始化二维数组。

第一种方法:

int x[3][4] = {0, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}

上面的数组有3行4列。括号中从左到右的元素也从左到右存储在表中。元素将按以下顺序填充到数组中, 第一行从左开始的前4个元素, 第二行从下4个元素, 依此类推。

更好的方法:

int x[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};

这种类型的初始化使用嵌套的花括号。每组内部括号代表一行。在上面的示例中, 总共有三行, 因此有三组内部括号。

访问二维数组的元素:使用行索引和列索引访问二维数组中的元素。

例子:

int x[2][1];

上面的示例表示第三行和第二列中存在的元素。

注意:在数组中, 如果数组的大小为N。其索引将为0到N-1。因此, 对于行索引2, 行号为2 + 1 = 3。

要输出二维数组的所有元素, 我们可以使用嵌套的for循环。我们将需要两个for循环。一个遍历行, 另一个遍历列。

// C++ Program to print the elements of a
// Two-Dimensional array
#include<iostream>
using namespace std;
  
int main()
{
     // an array with 3 rows and 2 columns.
     int x[3][2] = {{0, 1}, {2, 3}, {4, 5}};
  
     // output each array element's value
     for ( int i = 0; i < 3; i++)
     {
         for ( int j = 0; j < 2; j++)
         {
             cout << "Element at x[" << i
                  << "][" << j << "]: " ;
             cout << x[i][j]<<endl;
         }
     }
  
     return 0;
}

输出如下:

Element at x[0][0]: 0
Element at x[0][1]: 1
Element at x[1][0]: 2
Element at x[1][1]: 3
Element at x[2][0]: 4
Element at x[2][1]: 5

三维数组

C/C++中的多维数组1

初始化三维数组:三维数组的初始化与二维数组的初始化相同。区别在于尺寸数增加, 因此嵌套括号的数量也将增加。

方法1:

int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};

更好的方法:

int x[2][3][4] = 
 { 
   { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} }, { {12, 13, 14, 15}, {16, 17, 18, 19}, {20, 21, 22, 23} }
 };

访问三维数组中的元素:访问三维数组中的元素也类似于二维数组。不同之处在于, 对于三维数组中的另一维, 我们必须使用三个循环而不是两个循环。

// C++ program to print elements of Three-Dimensional
// Array
#include<iostream>
using namespace std;
  
int main()
{
     // initializing the 3-dimensional array
     int x[2][3][2] =
     {
         { {0, 1}, {2, 3}, {4, 5} }, { {6, 7}, {8, 9}, {10, 11} }
     };
  
     // output each element's value
     for ( int i = 0; i < 2; ++i)
     {
         for ( int j = 0; j < 3; ++j)
         {
             for ( int k = 0; k < 2; ++k)
             {
                 cout << "Element at x[" << i << "][" << j
                      << "][" << k << "] = " << x[i][j][k]
                      << endl;
             }
         }
     }
     return 0;
}

输出如下:

Element at x[0][0][0] = 0
Element at x[0][0][1] = 1
Element at x[0][1][0] = 2
Element at x[0][1][1] = 3
Element at x[0][2][0] = 4
Element at x[0][2][1] = 5
Element at x[1][0][0] = 6
Element at x[1][0][1] = 7
Element at x[1][1][0] = 8
Element at x[1][1][1] = 9
Element at x[1][2][0] = 10
Element at x[1][2][1] = 11

以类似的方式, 我们可以创建任意维数的数组。但是, 复杂度也随着维数的增加而增加。

最常用的多维数组是二维数组。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: