修改密码

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

成品课程

陈远龙老师主讲 & 答疑

课程题单 - T1000

未购买 · 可先试学25节课

课程目录展开/折叠

第31课 二维数组

播放快捷键

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

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

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

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

本节课讲解到的源代码

源代码下载:第31课 二维数组-源代码下载

1. 二维数组-1
#include <bits/stdc++.h>
using namespace std;

// int a[3][4]; // 全局变量, 全部默认初始化为0 

int main()
{

    // 定义个二维数组

    int a[3][4]; // 函数中定义的,使用的是栈内存空间 

    // 打印下二维数组里的内容
    // 第一种思路:行的思路:
    // a[0] : . . . .
    // a[1]:  . . . .
    // a[2]:  . . . . 

    // 第二种思路:
    // a[0][0] a[0][1] a[0][2] a[0][3]
    // a[1][0] a[1][1] a[1][2] a[1][3]
    // a[2][0] a[2][1] a[2][2] a[2][3]
    for (int i = 0; i < 3; i ++)
    {
        // a[0]行 a[1]行 a[2]行 看作是一维数组 
        for (int j = 0; j < 4; j ++)
        {
            cout << a[i][j] << ' ';
        }
        cout << endl;
    }

    return 0;
} 
2. 二维数组-2
#include <bits/stdc++.h>
using namespace std;

int main()
{
    /*
    // 一维数组的初始化 
    int b[10] = {1, 2, 3};
    int c[10] = {0};
    int d[10];
    for (int i = 0; i < 10; i ++)
        cin >> d[i];
    */

    // 定义个二维数组
    // 函数中定义的,使用的是栈内存空间 
    /*
    int a[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    } ; 
    */

    // 二维数组的部分初始化 
    /*
    int a[3][4] = {
        {1, 2},
        {5},
        {9, 10, 11, 12}
    }; 
    */

    /*
    int a[3][4] = {
        {0},
        {0},
        {0}
    };
    */

    // int a[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    // int a[3][4] = {1, 2, 3};
    // int a[3][4] = {0};

    int a[3][4];
    for (int i = 0; i < 3; i ++)
    {
        for (int j = 0; j < 4; j ++)
        {
            cin >> a[i][j];
        }
    }

    for (int i = 0; i < 3; i ++)
    {
        for (int j = 0; j < 4; j ++)
            cout << a[i][j] << ' ';

        cout << endl;
    }

    return 0;
} 
3. 二维数组-3
#include <bits/stdc++.h>
using namespace std;

int main()
{

    /*
    char b[] = {'c', 'h', 'e', 'n'};
    cout << sizeof(b) << endl;
    cout << sizeof b << endl;

    char c = 'm';
    cout << sizeof(c) << endl;
    cout << sizeof c << endl;
    cout << sizeof(char) << endl;
    // cout << sizeof char << endl;
    */

    // 二维数组存储的时候:按行优先存储的规则 
    // 二维数组定义的时候必须指定列数 
    /*
    int a[][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };

    // int b[3][] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    */

    // int a[][4] = {1, 2, 3};
    // int a[][4] = {1, 2, 3, 4};
    // int a[][4] = {1, 2, 3, 4, 5};
    // int a[][4] = {1, 2, 3, 4, 5, 6, 7, 8};

    // sizeof验证
    /* 
    int a[3][4];
    cout << sizeof(a) << endl;
    */

    // int a[][4] = {1, 2, 3};
    // int a[][4] = {1, 2, 3, 4};
    /*
    int a[][4] = {1, 2, 3, 4, 5};
    cout << sizeof(a) << endl;
    */

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

    cout << sizeof(a) << endl;

    return 0;
} 
4. 二维数组-4

#include <bits/stdc++.h>
using namespace std;

int main()
{
    /*
    int a[3][4] = {0};

    // 1个元素占用的空间(字节) * 总元素个数(个数) = 占用的总空间(字节) 
    cout << sizeof(a) / sizeof(int) << endl; // 12个元素
    cout << sizeof(a) / sizeof(a[0][0]) << endl; // 12个元素
    */

    int a[][4] = {1, 2, 3, 4, 5};
    cout << sizeof(a) / sizeof(a[0][0]) << endl; // 8个元素 

    // 计算行数
    // 每一行占用的空间 * 总行数 = 占用的总空间
    cout << sizeof(a) / (4 * sizeof(int)) << endl; // 2行 

    // sizeof(a[0]) => 每一行占用的空间
    cout << sizeof(a) / sizeof(a[0]) << endl; // 2行 

    // 计算列数
    // 列数 * 每个元素占用的空间 = 一行占用的空间 
    cout << sizeof(a[0]) / sizeof(a[0][0]) << endl; // 4列 
    cout << sizeof(a[0]) / sizeof(int) << endl; // 4列 

    return 0;
} 

本节课答疑

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

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

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

目录