Wiki
Array
Existing English translations were reused. 2 current source blocks remain in Simplified Chinese, without triggering paid translation.
1.array
1.1.Overview
A so-called array is a collection that stores data elements of the same type.
**Feature 1:** Each ==data element in the array is of the same data type==
**Feature 2:** An array is composed of ==contiguous memory== locations.

1.2.one-dimensional array
1.2.1.One-dimensional array definition methods
Three ways to define a one-dimensional array:
数据类型 数组名[ 数组长度 ];数据类型 数组名[ 数组长度 ] = { 值1,值2 ...};数据类型 数组名[ ] = { 值1,值2 ...};
Example
int main() {
//定义方式1
//数据类型 数组名[元素个数];
int score[10];
//利用下标赋值
score[0] = 100;
score[1] = 99;
score[2] = 85;
//利用下标输出
cout << score[0] << endl;
cout << score[1] << endl;
cout << score[2] << endl;
//第二种定义方式
//数据类型 数组名[元素个数] = {值1,值2 ,值3 ...};
//如果{}内不足10个数据,剩余数据用0补全
int score2[10] = { 100, 90,80,70,60,50,40,30,20,10 };
//逐个输出
//cout << score2[0] << endl;
//cout << score2[1] << endl;
//一个一个输出太麻烦,因此可以利用循环进行输出
for (int i = 0; i < 10; i++)
{
cout << score2[i] << endl;
}
//定义方式3
//数据类型 数组名[] = {值1,值2 ,值3 ...};
int score3[] = { 100,90,80,70,60,50,40,30,20,10 };
for (int i = 0; i < 10; i++)
{
cout << score3[i] << endl;
}
return 0;
}
Operation result:
100
99
85
100
90
80
70
60
50
40
30
20
10
100
90
80
70
60
50
40
30
20
10
Summary 1: The naming conventions for array names are consistent with those for variable names; do not reuse variable names.
Summary 2: In arrays, indexing starts from 0
1.2.2.One-dimensional array array name
The name of a one-dimensional arrayPurpose:
- Can count the length of the entire array in memory
- Can obtain the starting address of the array in memory
Example:
int main() {
//数组名用途
//1、可以获取整个数组占用内存空间大小
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
cout << "整个数组所占内存空间为: " << sizeof(arr) << endl;
cout << "每个元素所占内存空间为: " << sizeof(arr[0]) << endl;
cout << "数组的元素个数为: " << sizeof(arr) / sizeof(arr[0]) << endl;
//2、可以通过数组名获取到数组首地址
cout << "数组首地址为: " << static_cast<const void*>(arr) << endl;
cout << "数组中第一个元素地址为: " << &arr[0] << endl;
cout << "数组中第二个元素地址为: " << &arr[1] << endl;
//arr = 100; 错误,数组名是常量,因此不可以赋值
return 0;
}
Running results (the address may differ each run; below, <地址> is used to represent it; type sizes or type names may vary with the compiler and platform):
整个数组所占内存空间为: 40
每个元素所占内存空间为: 4
数组的元素个数为: 10
数组首地址为: <地址>
数组中第一个元素地址为: <地址>
数组中第二个元素地址为: <地址>
Note: The array name is a constant and cannot be assigned.
Summary 1: Directly printing the array name allows you to view the starting address of the memory occupied by the array.
Summary 2: Applying sizeof to an array name yields the size of the memory space occupied by the entire array.
Practice Case 1: Five Little Pigs Weigh Themselves
案例描述:
在一个数组中记录了五只小猪的体重,如:int arr[5] = {300,350,200,400,250};
Find and print the weight of the heaviest piglet.
**Practice Case 2:** Reversing array elements
**Case description:** Please declare an array of 5 elements and reverse the elements.
(For example, if the original array elements are: 1,3,2,5,4; after reversing, the output result is: 4,5,2,3,1);
1.2.3.Bubble sort
Function: The most commonly used sorting algorithm, which sorts the elements within an array
- Compare adjacent elements. If the first is greater than the second, swap them.
- Do the same work for each pair of adjacent elements; after execution, the first maximum value is found.
- Repeat the above steps, decreasing the number of comparisons by 1 each time, until no comparisons are needed.

Example: Sort the array { 4,2,8,0,5,7,1,3,9 } in ascending order
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
int arr[9] = { 4,2,8,0,5,7,1,3,9 };
for (int i = 0; i < 9 - 1; i++)
{
for (int j = 0; j < 9 - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < 9; i++)
{
cout << arr[i] << endl;
}
// 返回 0 表示程序正常结束。
return 0;
}
Operation result:
0
1
2
3
4
5
7
8
9
1.3.two-dimensional array
A two-dimensional array is simply a one-dimensional array with an additional dimension.

1.3.1.Definition methods of two-dimensional arrays
Four ways to define a two-dimensional array:
数据类型 数组名[ 行数 ][ 列数 ];数据类型 数组名[ 行数 ][ 列数 ] = { {数据1,数据2 } ,{数据3,数据4 } };数据类型 数组名[ 行数 ][ 列数 ] = { 数据1,数据2,数据3,数据4};数据类型 数组名[ ][ 列数 ] = { 数据1,数据2,数据3,数据4};
Suggestion: Among the above 4 definition methods, ==the second one is more intuitive and improves code readability==
Example:
int main() {
//方式1
//数组类型 数组名 [行数][列数]
int arr[2][3];
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 4;
arr[1][1] = 5;
arr[1][2] = 6;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
//方式2
//数据类型 数组名[行数][列数] = { {数据1,数据2 } ,{数据3,数据4 } };
int arr2[2][3] =
{
{1,2,3},
{4,5,6}
};
//方式3
//数据类型 数组名[行数][列数] = { 数据1,数据2 ,数据3,数据4 };
int arr3[2][3] = { 1,2,3,4,5,6 };
//方式4
//数据类型 数组名[][列数] = { 数据1,数据2 ,数据3,数据4 };
int arr4[][3] = { 1,2,3,4,5,6 };
return 0;
}
Operation result:
1 2 3
4 5 6
Summary: When defining a two-dimensional array, if data is initialized, the number of rows can be omitted.
1.3.2.Two-dimensional array array name
- View the memory space occupied by a two-dimensional array
- Get the base address of a two-dimensional array
Example:
int main() {
//二维数组数组名
int arr[2][3] =
{
{1,2,3},
{4,5,6}
};
cout << "二维数组大小: " << sizeof(arr) << endl;
cout << "二维数组一行大小: " << sizeof(arr[0]) << endl;
cout << "二维数组元素大小: " << sizeof(arr[0][0]) << endl;
cout << "二维数组行数: " << sizeof(arr) / sizeof(arr[0]) << endl;
cout << "二维数组列数: " << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
//地址
cout << "二维数组首地址:" << arr << endl;
cout << "二维数组第一行地址:" << arr[0] << endl;
cout << "二维数组第二行地址:" << arr[1] << endl;
cout << "二维数组第一个元素地址:" << &arr[0][0] << endl;
cout << "二维数组第二个元素地址:" << &arr[0][1] << endl;
return 0;
}
Running results (the address may differ each run; below, <地址> is used to represent it; type sizes or type names may vary with the compiler and platform):
二维数组大小: 24
二维数组一行大小: 12
二维数组元素大小: 4
二维数组行数: 2
二维数组列数: 3
二维数组首地址:<地址>
二维数组第一行地址:<地址>
二维数组第二行地址:<地址>
二维数组第一个元素地址:<地址>
二维数组第二个元素地址:<地址>
Summary 1: The name of a two-dimensional array is the base address of that array
Summary 2: When applying sizeof to the name of a two-dimensional array, you can obtain the size of the memory occupied by the entire two-dimensional array
1.3.3.Two-Dimensional Array Application Case
Exam score statistics:
Case description: There are three students (Zhang San, Li Si, Wang Wu), and their scores in an exam are as shown in the following table,Please output the total scores of the three students separately.
| Chinese | Mathematics | English | |
|---|---|---|---|
| Zhang San | 100 | 100 | 100 |
| Li Si | 90 | 50 | 100 |
| Wang Wu | 60 | 70 | 80 |
Reference answer:
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
int scores[3][3] =
{
{100,100,100},
{90,50,100},
{60,70,80},
};
string names[3] = { "张三","李四","王五" };
for (int i = 0; i < 3; i++)
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += scores[i][j];
}
cout << names[i] << "同学总成绩为: " << sum << endl;
}
// 返回 0 表示程序正常结束。
return 0;
}
Operation result:
张三同学总成绩为: 300
李四同学总成绩为: 240
王五同学总成绩为: 210