• C++学习笔记(Ⅰ):C++基础入门


    视频链接:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili

    目录

    1 C++初识

    1.1 第一个C++程序

     1.2 注释

    1.3 变量

    1.4 常量

    1.5 关键字

    1.6 标识符命名规则

    2 数据类型

    2.1 整型

    2.2 sizeof

    2.3 实型(浮点型)

    2.4 字符型

    2.5 转义字符

    2.6 字符串 

    2.7 布尔类型

    2.8 数据输入

    3 运算符

    3.1 算术运算符

    3.2 赋值运算符

    3.3 比较运算符

    3.4 逻辑运算符

    4 程序流程结构

    4.1 选择结构

    1.if语句

    2.三目运算符

    3.switch语句

    4.2 循环语句

    1.whle

    2.练习

    3.do while

    4.练习

    5.for循环

    6.练习

    7.嵌套循环

    8.练习

    4.3 跳转语句

    5 数组

    5.1 概述

    5.2 一维数组

    1.定义方式

    2.数组名称

    3.练习

    4.冒泡排序

    5.3 二维数组

    1.定义方式

    2.数组名称

    3.练习

    6 函数

    6.1 函数定义

    6.2 函数调用

    6.3 值传递

    6.4 常见样式

    6.5 函数声明

    6.6 函数的分文件编写

    7 指针

    7.1 指针定义

    7.2 空指针和野指针

    1.空指针

    2.野指针

    7.3 const修饰指针

    1.const修饰指针--常量指针

    2.const修饰常量--指针常量

    3.const

    7.4 指针和数组

    7.5 指针和函数

    7.6 练习

    8 结构体

    8.1 结构体定义

    8.2 结构体数组

    8.3 结构体指针

    8.4 结构体嵌套

    8.5 结构体做函数参数

    8.6 const

    8.7 练习


    1 C++初识

    1.1 第一个C++程序

     C++可使用的IDE包括经典的DEV C++、VS Code、VS、Clion等,这里因前期Python编译主要使用VS Code进行,且VS所需内存实在过大,因此仍选择VS Code作为IDE。输出第一个代码“Hello World”

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. cout << "hello world" << endl;
    6. system("pause");
    7. return 0;
    8. }

     1.2 注释

    // 单行注释

    /* `````` */ 多行注释

    main是程序入口,在源文件中必须存在且仅有一个。程序只运行main函数

    1.3 变量

    作用:给一段指定内存空间起名,以方便操作

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. int a = 10;
    6. cout << "a = " << a << endl;
    7. system("pause");
    8. return 0;
    9. }

    1.4 常量

    1. #include
    2. using namespace std;
    3. // #define 宏常量定义,不可修改
    4. // const 修改变量为常量
    5. #define Day 7
    6. int main()
    7. {
    8. // 1.宏常量
    9. cout << "一周总共有:" << Day << "天" << endl;
    10. // 2.const
    11. const int month = 12;
    12. cout << "一年总共有:" << month << "月" << endl;
    13. system("pause");
    14. return 0;
    15. }

    1.5 关键字

    C++中不得使用关键字作为变量名

    1.6 标识符命名规则

    1.标识符不能是关键字;

    2.只能由数字、字母、下划线组成

    3.首位必须是下划线或字母

    4.标识符中区分字母大小写

    2 数据类型

    2.1 整型

    short短整型(2byte)、int整型(4byte)、long长整型(Windows 4byte/Linux32位4byte/Linux64位8byte)、long long长长整型(8byte)

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. short num1 = 10;
    6. int num2 = 10;
    7. long num3 = 10;
    8. long long num4 = 10;
    9. cout << "num1 = " << num1 << endl;
    10. cout << "num2 = " << num2 << endl;
    11. cout << "num3 = " << num3 << endl;
    12. cout << "num4 = " << num4 << endl;
    13. system("pause");
    14. return 0;
    15. }

    2.2 sizeof

    统计数据类型所占内存大小

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // sizeof(数据类型/变量)
    6. short num1 = 10;
    7. int num2 = 10;
    8. long num3 = 10;
    9. long long num4 = 10;
    10. cout << "short内存: " << sizeof(num1) << endl;
    11. cout << "int内存: " << sizeof(int) << endl;
    12. cout << "long内存: " << sizeof(num3) << endl;
    13. cout << "long long内存: " << sizeof(num4) << endl;
    14. system("pause");
    15. return 0;
    16. }

    2.3 实型(浮点型)

    1.单精度float(4byte,7位有效数字)

    2.双精度double(8byte,15-16位有效数字)

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // 默认显示六位有效数字
    6. float f1 = 3.14f;
    7. double f2 = 3.14;
    8. cout << "f1: " << f1 << endl;
    9. cout << "f2: " << f2 << endl;
    10. // 科学计数法
    11. float f3 = 3e2;
    12. cout << "f3: " << f3 << endl;
    13. system("pause");
    14. return 0;
    15. }

    2.4 字符型

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // 只占用1个字节,只能使用1个字符
    6. // 将字符对应的ASCII编码送入存储单元
    7. char ch = 'a'; // 务必使用单引号
    8. cout << ch << endl;
    9. // 输出ch的ASCII编码
    10. cout << (int)ch << endl;
    11. system("pause");
    12. return 0;
    13. }

    2.5 转义字符

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // 表示不能显示的ASCII字符
    6. // 换行
    7. cout << "hello world\n";
    8. // 反斜杠
    9. cout << "\\" << endl;
    10. // 水平制表 \t占8个位
    11. cout << "aaa\thello" << endl;
    12. cout << "aa\thello" << endl;
    13. cout << "aaaa\thello" << endl;
    14. system("pause");
    15. return 0;
    16. }

    2.6 字符串 

    1. #include
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. // 1.C风格:char 变量名[] = "字符串值"
    7. char str[] = "hello world";
    8. cout << str << endl;
    9. // 2. C++风格:string 变量名 = "字符串值"
    10. // 需包含头文件 #include
    11. string str2 = "hello world";
    12. cout << str2 << endl;
    13. system("pause");
    14. return 0;
    15. }

    2.7 布尔类型

    true表示真(1),Flase表示假(0),占1个字节  

    2.8 数据输入

    从键盘获取数据:cin

    1. #include
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. // 定义变量
    7. int a = 0;
    8. // 设置输入
    9. cin >> a;
    10. cout << a << endl;
    11. system("pause");
    12. return 0;
    13. }

    使用ctrl + k + c 注释所选择的内容

    3 运算符

    3.1 算术运算符

    执行四则运算:+加、-减、*乘、/除

    %取余、前置/后置递增/递减

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // 四则运算,整数相除结果依旧为整数,去除小数部分
    6. int a = 1;
    7. int b = 2;
    8. cout << a + b << endl; // 3
    9. cout << a - b << endl; // -1
    10. cout << a * b << endl; // 2
    11. cout << a / b << endl; // 0
    12. // 取模运算,两小数不可进行取模运算
    13. int num1 = 10;
    14. int num2 = 3;
    15. cout << num1 % num2 << endl; // 1
    16. // 递增/减运算
    17. // 1.前置递增,先加1再运算
    18. int c = 10;
    19. ++c;
    20. cout << c << endl; // 11
    21. // 2.后置递增,先运算再加1
    22. int d = 10;
    23. d++;
    24. cout << d << endl; // 11
    25. system("pause");
    26. return 0;
    27. }

    3.2 赋值运算符

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // +=
    6. int a = 1;
    7. a += 2;
    8. cout << a<< endl;
    9. system("pause");
    10. return 0;
    11. }

    3.3 比较运算符

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // ==相等
    6. // !=不相等
    7. // <小于 >大于 <=小于等于 >=大于等于
    8. int a = 1;
    9. int b = 2;
    10. cout << (a == b) << endl; // ()里为优先运算
    11. system("pause");
    12. return 0;
    13. }

    3.4 逻辑运算符

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // !非
    6. int a = 1;
    7. cout << !a << endl; //0
    8. // && 与
    9. int b = 0;
    10. cout << (a && b) << endl; //0
    11. // || 或
    12. int c = 0;
    13. cout << (a||c) << endl; //1
    14. system("pause");
    15. return 0;
    16. }

    4 程序流程结构

    4.1 选择结构

    1.if语句

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // 顺序结构:按顺序执行程序,不跳转
    6. // 选择结构:单行if语句
    7. int score = 0;
    8. cout << "Input your score:" << endl;
    9. cin >> score;
    10. if (score > 600)
    11. {
    12. cout << "ok" << endl;
    13. }
    14. // 选择结构:if-else语句
    15. int score = 0;
    16. cout << "Input your score:" << endl;
    17. cin >> score;
    18. if (score > 600)
    19. {
    20. cout << "ok" << endl;
    21. }
    22. else
    23. {
    24. cout << "No" << endl;
    25. }
    26. // if多条件语句
    27. int score = 0;
    28. cout << "Input your score:" << endl;
    29. cin >> score;
    30. if (score > 600) {
    31. cout << "1" << endl;
    32. }
    33. else if (score > 500) {
    34. cout << "2" << endl;
    35. }
    36. else {
    37. cout << "3" << endl;
    38. }
    39. // 嵌套if语句
    40. int score = 0;
    41. cout << "Input your score:" << endl;
    42. cin >> score;
    43. if (score > 600) {
    44. cout << "1" << endl;
    45. if (score > 700) {
    46. cout << "0" << endl;
    47. }
    48. }
    49. system("pause");
    50. return 0;
    51. }

    exercise

    1. #include
    2. using namespace std;
    3. int main() {
    4. // 三个数比大小,比最大数
    5. int a = 0, b = 0, c = 0;
    6. cout << "Input a:" << endl;
    7. cin >> a;
    8. cout << "Input b:" << endl;
    9. cin >> b;
    10. cout << "Input c:" << endl;
    11. cin >> c;
    12. if (a > b) {
    13. if (a > c) {
    14. cout << "a MAX " << endl;
    15. }
    16. else {
    17. cout << "c MAX" << endl;
    18. }
    19. }
    20. else {
    21. if (b > c)
    22. {
    23. cout << "b MAX" << endl;
    24. }
    25. else
    26. {
    27. cout << "c MAX" << endl;
    28. }
    29. }
    30. system("pause");
    31. }

    2.三目运算符

    1. // 三目运算符,返回变量,可继续赋值
    2. int a = 10, b = 20, c = 0;
    3. c=(a > b ? a : b);
    4. cout << c << endl;

    3.switch语句

    1. // switch语句
    2. int score = 0;
    3. cin >> score;
    4. switch (score)
    5. {
    6. case 10 :
    7. cout << "10" << endl;
    8. break;
    9. case 9:
    10. cout << "9" << endl;
    11. break;
    12. case 8:
    13. cout << "8" << endl;
    14. break;
    15. default:
    16. cout << "<8" << endl;
    17. break;
    18. }

    4.2 循环语句

    1.whle

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. int num = 0;
    6. while (num < 10)
    7. {
    8. cout << num++ << endl;
    9. }
    10. system("pause");
    11. return 0;
    12. }

    2.练习

    1. #include
    2. using namespace std;
    3. #include
    4. int main()
    5. {
    6. // 随机数种子,利用系统时间生成随机数
    7. srand((unsigned int)time(NULL));
    8. // 猜测1-100间的数字
    9. // 1.生成随机数
    10. int num = rand() % 100 + 1; // 生成0+1~99+1的随机数
    11. // 2.玩家猜测
    12. int val = 0;
    13. while (1)
    14. {
    15. cin >> val;
    16. // 3. 判断猜测
    17. if (val > num)
    18. {
    19. cout << "Big" << endl;
    20. }
    21. else if (val < num)
    22. {
    23. cout << "Small" << endl;
    24. }
    25. else
    26. {
    27. cout << "Right" << endl;
    28. break;
    29. }
    30. }
    31. system("pause");
    32. return 0;
    33. }

    3.do while

    1. #include
    2. using namespace std;
    3. #include
    4. int main()
    5. {
    6. // do while,先执行再判断
    7. int num = 0;
    8. do
    9. {
    10. cout << num++ << endl;
    11. } while (num < 10);
    12. system("pause");
    13. return 0;
    14. }

    4.练习

    1. #include
    2. using namespace std;
    3. #include
    4. int main()
    5. {
    6. // 水仙花数,即3位数,个位上数字的三次幂之和等于其本身
    7. // 找到100--999的水仙花数
    8. int num = 100;
    9. do
    10. {
    11. int a, b, c = 0;
    12. a = num % 10;
    13. b = num / 10 % 10;
    14. c = num / 100;
    15. if (a*a*a + b*b*b + c*c*c == num)
    16. {
    17. cout << num << endl;
    18. }
    19. num++;
    20. }
    21. while (num < 1000);
    22. // 判断水仙花数
    23. system("pause");
    24. return 0;
    25. }

    5.for循环

    1. #include
    2. using namespace std;
    3. #include
    4. int main()
    5. {
    6. // for循环
    7. for (int i = 0; i < 10; i++)
    8. {
    9. cout << i << endl;
    10. }
    11. system("pause");
    12. return 0;
    13. }

    6.练习

    1. #include
    2. using namespace std;
    3. #include
    4. int main()
    5. {
    6. //逢7必过,1-100
    7. for (int i = 1;i < 101; i++)
    8. {
    9. if (i % 7 == 0 || i / 10 == 7 || i % 10 == 7)
    10. {
    11. cout << i << endl;
    12. }
    13. }
    14. system("pause");
    15. return 0;
    16. }

    7.嵌套循环

    1. #include
    2. using namespace std;
    3. #include
    4. int main()
    5. {
    6. // 嵌套循环,打印*图
    7. for (int i = 0; i < 10; i++)
    8. {
    9. for (int j = 0; j < 10; j++)
    10. {
    11. cout << "* ";
    12. }
    13. cout << endl;
    14. }
    15. system("pause");
    16. return 0;
    17. }

    8.练习

    1. #include
    2. using namespace std;
    3. #include
    4. int main()
    5. {
    6. // 乘法口诀表
    7. for (int i = 1; i < 10; i++)
    8. {
    9. for (int j = 1; j < 10; j++)
    10. {
    11. if (j <= i)
    12. {
    13. cout << i*j << " \t";
    14. }
    15. }
    16. cout << endl;
    17. }
    18. system("pause");
    19. return 0;
    20. }

    4.3 跳转语句

    1.break跳出循环

    2.continue不执行后续循环

    3.goto语句跳转至标记位置

    5 数组

    5.1 概述

    1.数组中每个数据元素都是相同的数据类型,是由连续内存组成的

    5.2 一维数组

    1.定义方式

    数据类型 数组名[数组长度];

    数据类型 数组名[数组长度]={值};用0填补未赋值的元素

    数据类型 数组名[]={值};

    下标从0开始

    2.数组名称

    统计数组在内存中的长度,并可获取数组在内存中的首地址。

    3.练习

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // 练习1 五只小猪体重
    6. int arr[5] = { 300,350,200,400,250 };
    7. int max = 0;
    8. for (int i = 0; i < 5; i++)
    9. {
    10. if (arr[i] > max)
    11. {
    12. max = arr[i];
    13. }
    14. }
    15. cout << max << endl;
    16. system("pause");
    17. return 0;
    18. }
    19. #include
    20. using namespace std;
    21. int main()
    22. {
    23. // 练习2 数组元素逆置
    24. int arr[5] = { 1,3,2,5,4 };
    25. int start = 0;
    26. int end = sizeof(arr) / sizeof(arr[0]) - 1;
    27. while (start < end)
    28. {
    29. int temp = arr[start];
    30. arr[start] = arr[end];
    31. arr[end] = temp;
    32. start++;
    33. end--;
    34. }
    35. for (int i = 0; i < 5; i++)
    36. {
    37. cout << arr[i] << endl;
    38. }
    39. system("pause");
    40. return 0;
    41. }

    4.冒泡排序

    排序总轮数 = 元素个数 - 1

    每轮对比次数 = 元素个数 - 排序轮数 - 1

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // 冒泡排序实现升序排列
    6. int arr[] = { 4,2,8,0,5,7,1,3,9 };
    7. // 输出排序前数组元素
    8. for (int i = 0; i < 9; i++)
    9. {
    10. cout << arr[i] << " ";
    11. }
    12. cout << endl;
    13. // 冒泡排序
    14. for (int i = 0; i < 9 - 1; i++)
    15. {
    16. // 内层循环
    17. for (int j = 0; j < 9 - i - 1; j++)
    18. {
    19. if (arr[j] > arr[j + 1])
    20. {
    21. int temp = arr[j];
    22. arr[j] = arr[j + 1];
    23. arr[j + 1] = temp;
    24. }
    25. }
    26. }
    27. // 排序后
    28. for (int i = 0; i < 9; i++)
    29. {
    30. cout << arr[i] << " ";
    31. }
    32. cout << endl;
    33. system("pause");
    34. return 0;
    35. }

    5.3 二维数组

    1.定义方式

    数据类型 数组名称[行数][列数] = { {元素},{元素} }

    2.数组名称

    通过数组名称可以查看所占内存空间及二维数组首地址。 

    查看具体元素的地址需使用取址符&

    3.练习

    1. #include
    2. using namespace std;
    3. #include
    4. int main()
    5. {
    6. // 已知三同学单科成绩,分别输出三同学总成绩
    7. int score[3][3] = { {100,100,100},{90,50,100},{60,70,80} };
    8. string name[3] = { "zhangsan","lisi","wangwu" };
    9. for (int i = 0; i < 3; i++)
    10. {
    11. int sum = 0;
    12. for (int j = 0; j < 3; j++)
    13. {
    14. sum += score[i][j];
    15. }
    16. cout <
    17. }
    18. system("pause");
    19. return 0;
    20. }

    6 函数

    6.1 函数定义

    1. #include
    2. using namespace std;
    3. int add(int num1, int num2) {
    4. int sum = num1 + num2;
    5. cout << sum << endl;
    6. return sum;
    7. }

    6.2 函数调用

    1. #include
    2. using namespace std;
    3. int add(int num1, int num2) {
    4. // num1.num2称为形式参数
    5. int sum = num1 + num2;
    6. cout << sum << endl;
    7. return sum;
    8. }
    9. int main() {
    10. // a,b称为实际参数
    11. int a = 1;
    12. int b = 50;
    13. add(a,b);
    14. // 函数调用时,实参的值会传递给形参
    15. system("pause");
    16. return 0;
    17. }

    6.3 值传递

    值传递,即函数调用时将实参的值传入形参,形参改变时不会影响实参。

    函数若无需返回值,数据类型选用void

    6.4 常见样式

    1.无参无返

    2.有参无返

    3.无参有返

    4.有参有返

    6.5 函数声明

    程序按顺序执行,没用定义函数前自定义函数不能写在主函数之后。

    声明可以重复,但定义只能唯一。 

    6.6 函数的分文件编写

    1.创建后缀名.h的头文件

    2.创建.cpp的源文件

    3.在头文件中写函数声明

    4.在源文件中写函数定义

    7 指针

    通过指针可以间接访问内存

    7.1 指针定义

    数据类型 *  指针变量; 

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // 1.定义指针
    6. int a = 10;
    7. int * p;
    8. // 2.指针记录变量地址
    9. p = &a;
    10. cout <<"a的地址:"<< &a << endl;
    11. cout << "p为:" << p << endl;
    12. // 3.使用指针:解引用操作,找到指针指向内存中的数据
    13. *p = 1000;
    14. cout << "a=" << a << endl;
    15. cout << "*p=" << *p << endl;
    16. system("pause");
    17. return 0;
    18. }

    32位操作系统指针所占空间为4字节 

    7.2 空指针和野指针

    1.空指针

    指针变量指向内存中编号为0的空间,用于初始化指针变量,该内存由系统占据不可访问

    int * p = NULL;

    2.野指针

    指针变量指向非法空间,需避免出现。

    7.3 const修饰指针

    1.const修饰指针--常量指针

    const int * p = &a;

    指针的指向可以修改,但指向的值不可修改。 

    2.const修饰常量--指针常量

    int * const p = &a;

    指针的指向不可改,但指向的值可以改。

    3.const

    const int * const p = &a;

    指针的指向和指向的值都不可以改。

    7.4 指针和数组

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. // 创建数组,利用指针访问数组中元素
    6. int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
    7. int * p = arr;
    8. cout << "首元素" << *p << endl;
    9. p++;
    10. cout << "第二个元素" << *p << endl;
    11. system("pause");
    12. return 0;
    13. }

    7.5 指针和函数

    利用指针作为函数参数,可以修改实参的值。

    1. #include
    2. using namespace std;
    3. // 实现两个数字交换
    4. void swap(int *a, int *b)
    5. {
    6. int temp = *a;
    7. *a = *b;
    8. *b = temp;
    9. cout << a << " " << b << endl;
    10. }
    11. int main()
    12. {
    13. int a = 10;
    14. int b = 20;
    15. // 地址传递
    16. swap(&a, &b);
    17. cout << a <<" "<< b << endl;
    18. system("pause");
    19. return 0;
    20. }

    7.6 练习

    利用指针将数组传入函数中

    1. #include
    2. using namespace std;
    3. // 封装函数,利用冒泡排序,实现对整型数组的升序排序
    4. // 冒泡排序函数
    5. void bubbleSort(int *arr,int len)
    6. {
    7. for (int i = 0; i < len - 1; i++)
    8. {
    9. for (int j = 0; j < len - i - 1; j++)
    10. {
    11. if (arr[j] > arr[j + 1])
    12. {
    13. int temp = arr[j];
    14. arr[j] = arr[j + 1];
    15. arr[j + 1] = temp;
    16. }
    17. }
    18. }
    19. }
    20. int main()
    21. {
    22. // 1.创建数组
    23. int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
    24. int len = sizeof(arr) / sizeof(arr[0]);
    25. // 2.创建函数
    26. bubbleSort(arr, len);
    27. // 3.打印数组
    28. for (int i=0; i < 10; i++)
    29. {
    30. cout << arr[i];
    31. }
    32. cout << endl;
    33. system("pause");
    34. return 0;
    35. }

    8 结构体

    8.1 结构体定义

    属于用户自定义的数据类型,用于存储不同的数据类型。属于一些类型的集合(类似Python的字典?)

    struct 结构体名 变量名

    struct 结构体名 变量名 = {...}

    结构体创建时,struct关键字可以省略

    1. #include
    2. using namespace std;
    3. #include
    4. // 结构体定义
    5. struct student
    6. {
    7. string name;
    8. int age;
    9. int score;
    10. };
    11. int main()
    12. {
    13. // 结构体创建方式1
    14. struct student stu1;
    15. stu1.name = "pink";
    16. stu1.age = 18;
    17. stu1.score = 60;
    18. cout << stu1.name << stu1.age << stu1.score << endl;
    19. // 结构体创建方式2
    20. struct student stu2 = { "white",20,100 };
    21. cout << stu2.name << stu2.age << stu2.score << endl;
    22. system("pause");
    23. return 0;
    24. }

    8.2 结构体数组

    将自定义的结构体送入数组中

    struct 结构体名 数组名[元素数] ={{},{},..{}}

    1. #include
    2. using namespace std;
    3. #include
    4. // 结构体数组
    5. // 1.定义结构体
    6. struct student
    7. {
    8. string name;
    9. int age;
    10. int score;
    11. };
    12. int main()
    13. {
    14. // 2.创建结构体数组
    15. student stuarray[3]=
    16. {
    17. {"white",18,100},
    18. {"pink",20,60},
    19. {"yellow",16,75}
    20. };
    21. // 3.遍历结构体元素
    22. for (int i = 0; i < 3; i++)
    23. {
    24. cout << stuarray[i].name << stuarray[i].age << stuarray[i].score << endl;
    25. }
    26. system("pause");
    27. return 0;
    28. }

    8.3 结构体指针

    利用->通过结构体指针访问结构体属性

    1. #include
    2. using namespace std;
    3. #include
    4. // 结构体指针
    5. struct student
    6. {
    7. string name;
    8. int age;
    9. int score;
    10. };
    11. int main()
    12. {
    13. // 创建学生结构体变量
    14. student stu = { "zhangsan",15,100 };
    15. // 通过指针指向结构体变量
    16. student *p = &stu;
    17. // 通过指针访问结构体变量中的数据
    18. cout << p->name << p->age << p->score << endl;
    19. system("pause");
    20. return 0;
    21. }

    8.4 结构体嵌套

    1. #include
    2. using namespace std;
    3. #include
    4. // 结构体嵌套
    5. struct student
    6. {
    7. string name;
    8. int age;
    9. int score;
    10. };
    11. struct teacher
    12. {
    13. int id;
    14. string name;
    15. int age;
    16. struct student stu;
    17. };
    18. int main()
    19. {
    20. teacher t = { 10000,"white",50,"pink",20,60 };
    21. cout << t.name << t.id << t.age << t.stu.name << t.stu.age << t.stu.score << endl;
    22. system("pause");
    23. return 0;
    24. }

    8.5 结构体做函数参数

    将结构体作为参数向函数中传递,包括值传递和地址传递

    1. #include
    2. using namespace std;
    3. #include
    4. // 结构体函数传递
    5. struct student
    6. {
    7. string name;
    8. int age;
    9. int score;
    10. };
    11. // 1.值传递
    12. void printStudent(student stu)
    13. {
    14. cout << stu.name << stu.age << stu.score << endl;
    15. }
    16. // 2.地址传递
    17. void printStudent2(student * p)
    18. {
    19. cout << p->name << p->age << p->score << endl;
    20. }
    21. int main()
    22. {
    23. student stu = { "zhangsan",18,60 };
    24. printStudent(stu);
    25. printStudent2(&stu);
    26. system("pause");
    27. return 0;
    28. }

    8.6 const

    防止误操作

    1. #include
    2. using namespace std;
    3. #include
    4. // 结构体const
    5. struct student
    6. {
    7. string name;
    8. int age;
    9. int score;
    10. };
    11. // 将函数中形参改为指针,可以减少内存空间
    12. // const防止误操作
    13. void print(const student *stu)
    14. {
    15. cout << stu->name << stu->age << stu->score << endl;
    16. }
    17. int main()
    18. {
    19. student stu = { "jack",18,60 };
    20. print(&stu);
    21. system("pause");
    22. return 0;
    23. }

    8.7 练习

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. // 结构体案例:3个老师各带5个学生,完成毕业设计任务
    6. // 学生结构体
    7. struct student
    8. {
    9. string s_name;
    10. int score;
    11. };
    12. // 老师结构体
    13. struct teacher
    14. {
    15. string t_name;
    16. student sarray[5];
    17. };
    18. // 给老师和学生赋值的函数
    19. void allocateSpcae(teacher tarray[], int len)
    20. {
    21. string nameSeed = "ABCDE";
    22. // 给老师赋值
    23. for (int i = 0; i < len; i++)
    24. {
    25. tarray[i].t_name = "teacher_";
    26. tarray[i].t_name += nameSeed[i];
    27. // 给学生赋值
    28. for (int j = 0; j < 5; j++)
    29. {
    30. tarray[i].sarray[j].s_name = "student_";
    31. tarray[i].sarray[j].s_name += nameSeed[j];
    32. int random = rand() % 100;
    33. tarray[i].sarray[j].score = random;
    34. }
    35. }
    36. }
    37. void printInfo(teacher tarray[], int len)
    38. {
    39. for (int i = 0; i < len; i++)
    40. {
    41. cout << "老师姓名:" << tarray[i].t_name << endl;
    42. for (int j = 0; j < 5; j++)
    43. {
    44. cout << "\t学生姓名:" << tarray[i].sarray[j].s_name ;
    45. cout << " 学生成绩:" <
    46. }
    47. cout << endl;
    48. }
    49. }
    50. int main()
    51. {
    52. // 随机数种子
    53. srand((unsigned int)time(NULL));
    54. // 1.创建老师数组
    55. teacher tarray[3];
    56. // 2.利用函数给两个结构体赋值
    57. int len = sizeof(tarray) / sizeof(tarray[0]);
    58. allocateSpcae(tarray, len);
    59. // 3.打印信息
    60. printInfo(tarray, len);
    61. system("pause");
    62. return 0;
    63. }
    1. #include
    2. #include
    3. using namespace std;
    4. /* 设计一个包含任务姓名、年龄、性别,共存放五人的结构体
    5. 通过冒泡排序,将数组中的人按年龄进行排序并打印 */
    6. // 1.创建结构体
    7. struct heros
    8. {
    9. string name;
    10. int age;
    11. string gender;
    12. };
    13. void bubbleSort(heros array[], int len)
    14. {
    15. for (int i = 0; i < len; i++)
    16. {
    17. for (int j = 0; j < len - i - 1; j++)
    18. {
    19. if (array[j].age > array[j + 1].age)
    20. {
    21. heros temp = array[j];
    22. array[j] = array[j + 1];
    23. array[j + 1] = temp;
    24. }
    25. }
    26. }
    27. }
    28. int main()
    29. {
    30. // 2.创建结构体数组
    31. heros array[5] = {
    32. { "liubei", 23,"male"},
    33. { "guanyu", 22,"male" },
    34. { "zhangfei",20,"male" },
    35. { "zhaoyun", 21,"male" },
    36. { "diaochan",19,"female" },
    37. };
    38. for (int i = 0; i < 5; i++)
    39. {
    40. cout << "姓名:" <" "
    41. << "年龄:" << array[i].age << " "
    42. << "性别:"<< array[i].gender << " "
    43. << endl;
    44. }
    45. cout << endl;
    46. // 3.冒泡排序
    47. bubbleSort(array, 5);
    48. for (int i = 0; i < 5; i++)
    49. {
    50. cout << "姓名:" << array[i].name << " "
    51. << "年龄:" << array[i].age << " "
    52. << "性别:" << array[i].gender << " "
    53. << endl;
    54. }
    55. system("pause");
    56. return 0;
    57. }

  • 相关阅读:
    docker 部署Redis集群(三主三从,以及扩容、缩容)
    提升网络速度的几种有效方法
    【Canvas】js用Canvas绘制阴阳太极图动画效果
    OSI与TCP/IP 5层协议
    Arcgis pro通过渔网工具生成规则采样点,并对栅格数据进行采样
    基于GIS的生态安全网络格局构建之主成分分析
    【数据库SQL实战】获取所有部门当前manager的当前薪水情况
    设计模式-单例模式
    【毕业设计】19-基于单片机的物件计数控制系统设计(原理图工程+源代码工程+仿真工程+答辩论文)
    编写你的第一个java 程序
  • 原文地址:https://blog.csdn.net/m0_49939117/article/details/127331929