C/C++中的多维数组

阵列-基础 在C/C++中,我们可以简单地将多维数组定义为数组数组。多维数组中的数据以表格形式存储(按行的主要顺序)。 声明N维数组的一般形式:

null
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 typearray_name: Name of the arraysize1, 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] 可以存储总计(10*20)=200个元素。 相似数组 int x[5][10][20] 可以存储总计(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”如下所示:

two-d

初始化二维数组 :有两种方法可以初始化二维数组。 第一种方法 :

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循环。我们需要两个循环。一个用于遍历行,另一个用于遍历列。

CPP

// 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]: 0Element at x[0][1]: 1Element at x[1][0]: 2Element at x[1][1]: 3Element at x[2][0]: 4Element at x[2][1]: 5

三维阵列

图片[2]-C/C++中的多维数组-yiteyi-C++库

初始化三维阵列 :三维数组中的初始化与二维数组中的初始化相同。不同之处在于,随着尺寸标注数量的增加,嵌套大括号的数量也会增加。 方法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} } };

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

CPP

// 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] = 0Element at x[0][0][1] = 1Element at x[0][1][0] = 2Element at x[0][1][1] = 3Element at x[0][2][0] = 4Element at x[0][2][1] = 5Element at x[1][0][0] = 6Element at x[1][0][1] = 7Element at x[1][1][0] = 8Element at x[1][1][1] = 9Element at x[1][2][0] = 10Element at x[1][2][1] = 11

通过类似的方式,我们可以创建任意维数的数组。然而,随着维数的增加,复杂性也会增加。 最常用的多维数组是二维数组。

?列表=PLQM7ALHXFYSGG6GSRME2IN4K8FPH5QVB

本文由 严酷的阿加瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享