数组简介

C++C++Beginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

数组(Array)是一种存储在连续内存位置中的元素集合。数组中的每个元素都可以通过其索引值来访问。在本实验中,我们将学习如何在 C++ 中创建和操作数组。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/StandardLibraryGroup(["`Standard Library`"]) cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/arrays("`Arrays`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/IOandFileHandlingGroup -.-> cpp/user_input("`User Input`") cpp/StandardLibraryGroup -.-> cpp/standard_containers("`Standard Containers`") cpp/SyntaxandStyleGroup -.-> cpp/code_formatting("`Code Formatting`") subgraph Lab Skills cpp/variables -.-> lab-96237{{"`数组简介`"}} cpp/data_types -.-> lab-96237{{"`数组简介`"}} cpp/arrays -.-> lab-96237{{"`数组简介`"}} cpp/conditions -.-> lab-96237{{"`数组简介`"}} cpp/for_loop -.-> lab-96237{{"`数组简介`"}} cpp/output -.-> lab-96237{{"`数组简介`"}} cpp/user_input -.-> lab-96237{{"`数组简介`"}} cpp/standard_containers -.-> lab-96237{{"`数组简介`"}} cpp/code_formatting -.-> lab-96237{{"`数组简介`"}} end

包含所需库并定义命名空间

我们将在 ~/project 目录下使用以下命令创建一个名为 main.cpp 的新文件:

touch ~/project/main.cpp

我们首先包含必要的库,并使用 using namespace std; 语句定义 std 命名空间。

#include <iostream>
using namespace std

声明并初始化数组

在这一步中,我们将声明一个整数类型的数组,并用一些值对其进行初始化。

int main()
{
    int arr[5] = {8, 2, 3, 7, 1};
}

访问数组元素

在这一步中,我们将通过索引访问数组的元素。这里,我们将打印数组的第一个元素。

int main()
{
    int arr[5] = {8, 2, 3, 7, 1};
    cout << "The first element of the array is: " << arr[0] << endl;
}

修改数组元素

在这一步中,我们将通过为数组中的某个元素赋值来修改它。这里,我们将数组的第二个元素替换为新的值 5。

int main()
{
    int arr[5] = {8, 2, 3, 7, 1};

    // 修改数组的第二个元素
    arr[1] = 5;

    cout << "修改后的数组为: ";
    for (int i = 0; i < 5; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

从用户获取数组输入

在这一步中,我们将从用户那里获取数组元素作为输入。首先,我们会询问用户想要输入的元素数量。然后,我们将创建一个相应大小的数组,并用用户的输入填充它。

int main()
{
    int n;

    cout << "输入元素的数量: ";
    cin >> n;

    int arr[n];

    cout << "输入 " << n << " 个元素: ";
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }

    cout << "数组的元素为: ";
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

对数组进行排序

在这一步中,我们将使用冒泡排序算法(Bubble Sort)对数组进行升序排序。该算法通过重复交换相邻元素(如果它们的顺序错误)来实现排序。

int main()
{
    int arr[] = { 5, 7, 4, 6, 2 };

    int n = sizeof(arr) / sizeof(arr[0]);

    // 冒泡排序算法
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    cout << "排序后的数组为: ";
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

在终端中运行代码:

g++ main.cpp -o main
./main

总结

本实验涵盖了在 C++ 中使用数组的基础知识。我们学习了如何创建和初始化数组、访问和修改数组元素、获取用户输入,以及使用简单的冒泡排序算法对数组进行排序。通过这些知识,你应该能够有效地创建和操作 C++ 中的数组。

您可能感兴趣的其他 C++ 教程