在 C++ 中,数组是能够存储相同类型多个值的变量。例如:
假设一个班有 **27** 名学生,我们需要存储他们的所有成绩。与其创建 **27** 个单独的变量,不如直接创建一个数组。
double grade[27];
在此,grade 是一个可以容纳最多 27 个 double
类型元素的数组。
在 C++ 中,数组的大小和类型在声明后不能更改。
C++ 数组声明
dataType arrayName[arraySize];
例如,
int x[6];
这里,
int
- 要存储的元素的类型- x - 数组的名称
6
- 数组的大小
访问 C++ 数组的元素
在 C++ 中,数组中的每个元素都与一个数字相关联。这个数字称为 **数组索引**。我们可以使用这些索引来访问数组的元素。
// syntax to access array elements
array[index];
考虑上面我们看到的数组 x。

需要记住的一些事项
- 数组索引从 **0** 开始。这意味着 x[0] 是存储在索引 **0** 处的第一个元素。
- 如果数组的大小是
n
,则最后一个元素存储在索引(n-1)
处。在此示例中,x[5] 是最后一个元素。 - 数组的元素具有连续的地址。
例如,假设 x[0]
的起始地址是 **2120**。
那么,下一个元素 x[1]
的地址将是 **2124**,x[2]
的地址将是 **2128**,依此类推。
这里,每个元素的大小增加了 **4**。这是因为 int
的大小是 **4** 字节。
C++ 数组初始化
在 C++ 中,可以在声明时初始化数组。例如:
// declare and initialize an array
int x[6] = {19, 10, 8, 17, 9, 15};

声明时初始化数组的另一种方法
// declare and initialize an array
int x[] = {19, 10, 8, 17, 9, 15};
在这里,我们没有指定数组的大小。在这种情况下,编译器会自动计算大小。
C++ 具有空成员的数组
在 C++ 中,如果一个数组的大小是 n
,我们可以存储最多 n 个元素。但是,如果我们存储的元素少于 n
个会怎样?
例如,
// store only 3 elements in the array
int x[6] = {19, 10, 8};
这里,数组 x 的大小是 6
。但是,我们只用 3
个元素初始化了它。
在这种情况下,编译器会为剩余的位置分配随机值。通常,这个随机值就是 0
。

如何插入和打印数组元素?
int mark[5] = {19, 10, 8, 17, 9}
// change 4th element to 9
mark[3] = 9;
// take input from the user
// store the value at third position
cin >> mark[2];
// take input from the user
// insert at ith position
cin >> mark[i-1];
// print first element of the array
cout << mark[0];
// print ith element of the array
cout >> mark[i-1];
示例 1:显示数组元素
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {7, 5, 6, 12, 35};
cout << "The numbers are: ";
// Printing array elements
// using range based for loop
for (int n : numbers) {
cout << n << " ";
}
cout << "\nThe numbers are: ";
// Printing array elements
// using traditional for loop
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";
}
return 0;
}
输出
The numbers are: 7 5 6 12 35 The numbers are: 7 5 6 12 35
在这里,我们使用了 for 循环 从 i = 0
迭代到 i = 4
。在每次迭代中,我们都打印了 numbers[i]
。
我们再次使用基于范围的 for
循环来打印数组元素。要了解有关此循环的更多信息,请查看 C++ 范围 for 循环。
示例 2:从用户获取输入并将其存储在数组中
#include <iostream>
using namespace std;
int main() {
int numbers[5];
cout << "Enter 5 numbers: " << endl;
// store input from user to array
for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}
cout << "The numbers are: ";
// print array elements
for (int n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}
return 0;
}
输出
Enter 5 numbers: 11 12 13 14 15 The numbers are: 11 12 13 14 15
同样,我们使用 for
循环从 i = 0
迭代到 i = 4
。在每次迭代中,我们从用户那里获取输入并将其存储在 numbers[i]
中。
然后,我们使用另一个 for
循环打印所有数组元素。
C++ 数组越界
如果我们声明一个大小为 **10** 的数组,那么该数组将包含从索引 **0** 到 **9** 的元素。
但是,如果我们尝试访问索引为 **10** 或大于 **10** 的元素,这将导致未定义的行为。
当使用普通 for 循环并通过 []
运算符访问数组元素时,很可能会发生这种错误。
注意:为避免此类错误,我们应该优先使用 基于范围的 for 循环,其中无需使用任何此类运算符即可访问数组元素。
示例 3:使用 for 循环显示数组元素的总和和平均值
#include <iostream>
using namespace std;
int main() {
// initialize an array without specifying size
double numbers[] = {7, 5, 6, 12, 35, 27};
double sum = 0;
double count = 0;
double average;
cout << "The numbers are: ";
// print array elements
// use of range-based for loop
for (const double &n : numbers) {
cout << n << " ";
// calculate the sum
sum += n;
// count the no. of array elements
++count;
}
// print the sum
cout << "\nTheir Sum = " << sum << endl;
// find the average
average = sum / count;
cout << "Their Average = " << average << endl;
return 0;
}
输出
The numbers are: 7 5 6 12 35 27 Their Sum = 92 Their Average = 15.3333
在此程序中
- 我们初始化了一个
double
数组 numbers,但未指定其大小。我们还声明了三个double
变量:sum、count 和 average。
这里,sum = 0
且count = 0
。 - 然后我们使用基于范围的
for
循环打印数组元素。在循环的每次迭代中,我们将当前数组元素添加到 sum。 - 在每次迭代中,我们还将 count 的值增加
1
,以便在 for 循环结束时获得数组的大小。 - 打印完所有元素后,我们打印所有数字的总和和平均值。数字的平均值由
average = sum / count;
计算。
另请阅读