C++ 一维数组教程

学习数组的定义、初始化、访问和遍历

什么是数组?

数组是一组相同类型数据的集合,这些数据在内存中按顺序存放。

就像一排储物柜,每个柜子有编号(索引),可以存放物品(数据)。

数组示意图:

索引 0
85
索引 1
92
索引 2
78
索引 3
96
索引 4
88

这是一个包含5个整数的数组,存储了5个学生的成绩。

数组的定义

定义数组需要指定:数据类型、数组名、数组大小

// 语法:数据类型 数组名[数组大小];

// 示例:定义不同数据类型的数组
int scores[5]; // 包含5个整数的数组
double prices[10]; // 包含10个浮点数的数组
char letters[26]; // 包含26个字符的数组
bool flags[3]; // 包含3个布尔值的数组
重要:
  • 数组大小必须是整数常量
  • 数组索引从 0 开始,不是 1
  • 最后一个元素的索引是 数组大小-1

数组的初始化

在定义数组的同时给数组元素赋值

// 方法1:完全初始化(指定所有元素)
int numbers[5] = {10, 20, 30, 40, 50};

// 方法2:部分初始化(剩余元素自动为0)
int scores[5] = {85, 92, 78}; // scores[3]和scores[4]为0

// 方法3:自动确定数组大小
int data[] = {1, 2, 3, 4, 5}; // 数组大小自动为5

// 方法4:全部初始化为0
int zeros[10] = {0}; // 所有元素都是0

访问数组元素

使用数组名和索引来访问特定元素

// 定义并初始化数组
int arr[5] = {10, 20, 30, 40, 50};

// 读取数组元素
cout << "第一个元素: " << arr[0] << endl; // 输出 10
cout << "第三个元素: " << arr[2] << endl; // 输出 30

// 修改数组元素
arr[1] = 25; // 将第二个元素改为25
arr[4] = 60; // 将最后一个元素改为60

// 使用变量作为索引
int index = 3;
cout << "索引为3的元素: " << arr[index] << endl; // 输出 40
注意: 不要访问超出数组范围的元素!
arr[5] 在大小为5的数组中是非法的,会导致未定义行为。

使用循环遍历数组

循环是处理数组的最佳工具

// 示例1:使用for循环遍历数组
int numbers[5] = {10, 20, 30, 40, 50};

for (int i = 0; i < 5; i++) {
    cout << "numbers[" << i << "] = " << numbers[i] << endl;
}
输出结果:
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
// 示例2:计算数组元素的总和
int scores[5] = {85, 92, 78, 96, 88};
int sum = 0;

for (int i = 0; i < 5; i++) {
    sum += scores[i];
}

cout << "总分: " << sum << endl;
cout << "平均分: " << sum / 5.0 << endl;

实用案例

// 案例1:查找最高分
int scores[5] = {85, 92, 78, 96, 88};
int maxScore = scores[0]; // 假设第一个是最高分

for (int i = 1; i < 5; i++) {
    if (scores[i] > maxScore) {
        maxScore = scores[i];
    }
}

cout << "最高分: " << maxScore << endl;
// 案例2:数组反转
int numbers[6] = {1, 2, 3, 4, 5, 6};

// 打印原数组
cout << "原数组: ";
for (int i = 0; i < 6; i++) {
    cout << numbers[i] << " ";
}
cout << endl;

// 反转数组
for (int i = 0; i < 3; i++) { // 只需要交换前一半
    int temp = numbers[i];
    numbers[i] = numbers[5 - i];
    numbers[5 - i] = temp;
}

// 打印反转后的数组
cout << "反转后: ";
for (int i = 0; i < 6; i++) {
    cout << numbers[i] << " ";
}
cout << endl;

测试题

题目1:下面的代码输出什么?

int arr[4] = {5, 10, 15};
cout << arr[0] << " " << arr[2] << " " << arr[3];

答案:5 15 0

arr[0]=5, arr[2]=15, arr[3]没有被初始化,自动为0。

题目2:下面的代码有什么问题?

int n = 5;
int arr[n];
for (int i = 0; i <= n; i++) {
    arr[i] = i * 2;
}

两个错误:

  1. 数组大小不能是变量(n),必须是常量
  2. 循环条件 i <= n 会导致访问 arr[5],但有效索引是 0-4

题目3:写出下面代码的输出

int data[5] = {2, 4, 6, 8, 10};
for (int i = 4; i >= 0; i--) {
    cout << data[i] << " ";
}

答案:10 8 6 4 2

循环从最后一个元素开始,倒序输出数组元素。

编程练习

练习1:学生成绩统计

编写程序:

  • 定义包含10个学生成绩的数组
  • 计算并输出平均分
  • 找出并输出最高分和最低分
int scores[10] = {85, 92, 78, 96, 88, 65, 72, 90, 83, 79};
int sum = 0, maxScore = scores[0], minScore = scores[0];

for (int i = 0; i < 10; i++) {
    sum += scores[i];
    if (scores[i] > maxScore) maxScore = scores[i];
    if (scores[i] < minScore) minScore = scores[i];
}

cout << "平均分: " << sum / 10.0 << endl;
cout << "最高分: " << maxScore << endl;
cout << "最低分: " << minScore << endl;

练习2:数组元素查找

编写程序:

  • 定义包含8个整数的数组
  • 输入一个要查找的数字
  • 在数组中查找该数字,输出其位置(索引)
  • 如果没找到,输出"未找到"
int numbers[8] = {12, 45, 23, 67, 89, 34, 56, 78};
int target, position = -1;

cout << "请输入要查找的数字: ";
cin >> target;

for (int i = 0; i < 8; i++) {
    if (numbers[i] == target) {
        position = i;
        break;
    }
}

if (position != -1) {
    cout << "找到数字 " << target << " 在位置 " << position << endl;
} else {
    cout << "未找到数字 " << target << endl;
}

总结

重点回顾:
  • 数组是相同类型数据的集合
  • 数组索引从0开始,最大索引是大小-1
  • 使用循环可以方便地遍历数组
  • 不要访问超出数组范围的元素
  • 数组在内存中是连续存储的
题目 对/错/率 难度 记录 通过
姓名 分数 提交时间 操作