网站首页 > 基础教程 正文
在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]可存储总计(10 * 20)= 200个元素。
类似地,数组x[5][10][20]可以存储总计(5 * 10 * 20)= 1000个元素。
二维数组
二维数组是多维数组的最简单形式。我们可以将二维数组看作是一维数组的数组,以便于理解。
1)声明大小为x,y的二维数组的基本形式:
data_type array_name[x][y];
data_type: Type of data to be stored. Valid C/C++ data type.
2)我们可以将大小为10,20的二维整数数组“ x”声明为:
int x[10][20];
3)二维数组中的元素通常用x[i][j]表示,其中i是行号,“ j”是列号。
4)二维数组可以看作是具有“ x”行和“ y”列的表,其中行号的范围是0到(x-1),列号的范围是0到(y-1)。带有三行三列的二维数组“ 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
三维数组
初始化三维数组:三维数组中的初始化与二维数组中的初始化相同。不同之处在于,随着大小的增加,嵌套大括号的数量也会增加。
方法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
以类似的方式,我们可以创建任意维数的数组。但是,复杂度也随着维数的增加而增加。
最常用的多维数组是二维数组。
猜你喜欢
- 2024-11-12 C语言之一维数组 c语言一维数组排序
- 2024-11-12 总结系列合集:C++中的动态数组 c+ 动态数组
- 2024-11-12 C语言结构体,如何定义结构体数组?linux C第62讲
- 2024-11-12 数组不可以直接赋值,为什么结构体中的数组却可以?
- 2024-11-12 C/C++编程笔记:C数组、字符串常量和指针!三分钟弄懂它
- 2024-11-12 数据结构入门:数组介绍 数据结构之数组
- 2024-11-12 再说,数组 数组+数组
- 2024-11-12 c++入门教程:c++中的动态数组 c++动态数组怎么用
- 2024-11-12 c++数组指导 c++数组的定义与使用
- 2024-11-12 C++基础概念:指针与数组,指针运算,指针与机器物理地址
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- deletesql (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)