修改密码

【2024课程】零基础学会C++编程课程

成品课程

陈远龙老师主讲 & 答疑

课程题单 - T1000

未购买 · 可先试学25节课

课程目录展开/折叠

第26课 数组

播放快捷键

播放/暂停:空格(或鼠标单击)      全屏:F(或鼠标双击)      退出全屏:Esc

快进10 / 30 / 60秒:方向键→ / Ctrl + 方向键→ / Shift + 方向键→

快退10 / 30 / 60秒:方向键← / Ctrl + 方向键← / Shift + 方向键←

本节课讲解配套PPT&板书:

本节课讲解到的源代码

源代码下载:第26课 数组-源代码下载

1. 遍历数组
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a[10] = {1, 2, 3};
    for (int i = 0; i < 10; i ++)
    {
        cout << a[i] << ' ';
    }
    cout << endl;
    // 基于范围的for循环  C++11 C++2011
    for (int x : a)
    {
        cout << x << ' ';
    }
    cout << endl;
    cout << "-------------" << endl;

    // 遍历的时候修改数组中的元素 (可行) 
    /*
    for (int i = 0; i < 10; i ++)
    {
        a[i] = 2 * a[i];
        cout << a[i] << ' ';
    }
    cout << endl; 
    */   

    /* 这种方法不行 
    for (int x : a)
    {
        x = 2 * x;
    }

    for (int x : a)
    {
        cout << x << ' ';
    }
    */

    for (int &x : a)
    {
        x = 2 * x;
    } 
    for (int x : a)
    {
        cout << x << ' ';
    }
    cout << endl;
    for (int &x : a)
    {
        // x = 1;
        // x = 2 * x;
        cout << x << ' '; 
    }
    cout << endl;
    for (const int &x : a)
    {
        cout << x << ' ';
        // x = 100;
    }

    return 0;
} 
2. 引用类型
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a = 100;
    cout << a << endl;
    // int b = a;
    a = 200;
    cout << a << endl;

    // 引用类型
    int &a1 = a; // a1就是a的别名,小名 
    cout << a1 << endl; // 200
    a1 = 300;
    cout << a << endl; // 300

    int a2 = a;
    a2 = 400;
    cout << a << endl; // 300
    cout << a2 << endl; // 400

    cout << "-------" << endl;
    cout << &a << ' ' << &a1 << endl;

    cout << "----" << endl;
    int &aa = a;
    aa = 500;
    cout << a1 << endl; // 500

    cout << "-----" << endl;
    /* 编译出错 
    int &a3;
    a3 = a;
    */
    int &a4 = a1; // 内存模型 

    const int &a9 = a;
    cout << a9 << endl;
    // a9 = 900;
    a = 900;
    cout << a9 << endl;

    return 0;   
}
3. swap函数
#include <bits/stdc++.h>
using namespace std;

/*这个不行 
void swap1(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;
}
*/ 

/* 
void swap2(int *pa, int *pb)
{
    int temp = *pa;
    *pa = *pb;
    *pb = temp;   
}
*/

void swap3(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

int main()
{

    int a = 1, b = 2;
    // swap1(a, b);
    // cout << a << ' ' << b << endl;
    // swap2(&a, &b);
    // cout << a << ' ' << b << endl;
    swap3(a, b);
    cout << a << ' ' << b << endl;

    return 0;
}
4. 数组访问越界
#include <bits/stdc++.h>
using namespace std;

int main()
{
    // C 、C++天然的劣势,数组访问不做边界检查 
    // int b[1024 * 1024]; // 4MB
    int a[10]; // 0 - 9
    /*
    for (int i = 0; i < 10; i ++)
    {
        a[i] = i;
    } 
    */
    int cnt = 0;
    for (int &x : a)
    {
        x = cnt ++;
    }
    for (int &x : a)
    {
        cout << x << ' ';
    }

    // a[0] - a[9]

    cout << endl;
    cout << "----------" << endl;
    cout << a[10] << endl;
    a[10] = 900;
    cout << a[10] << endl;

    // cout << a[10000] << endl;
    // cout << "hello" << endl;

    cout << a[-1] << endl;

    return 0;
}
5. 调试内存
#include <bits/stdc++.h>
using namespace std;

int main()
{
    char c[10] = {'a', 'b', 'c', 'd'};

    int a[10] = {1, 2, 3, 4};

    a[1] = 0x12345678;

    return 0;
}

本节课答疑

建议大家有问题先通过AI答疑(比如:DeepSeek 等),AI时代需要学会使用AI辅助学习

陈远龙老师视频讲解:如何使用DeepSeek进行答疑?

通过AI未能获得满意解答的,可以联系陈远龙老师答疑

目录