• C++ 基础入门 之 结构体/结构体定义和使用/结构体数组/结构体指针/ 结构体嵌套结构体/结构体做函数参数/结构体中 const 使用场景/结构体案例


    C++ 基础入门 之 结构体/结构体定义和使用/结构体数组/结构体指针/ 结构体嵌套结构体/结构体做函数参数/结构体中 const 使用场景/结构体案例

    目录

    一、简单介绍

    二、结构体定义和使用

    三、结构体数组

    四、 结构体指针

    五、 结构体嵌套结构体

    六、结构体做函数参数

    七、结构体中 const 使用场景

    八、结构体案例 (打印出老师数据以及老师所带的学生数据)

    九、结构体案例 (打印出将数组中的英雄按照年龄进行升序冒泡排序后的结果)


    一、简单介绍

    C++ 开发的一些知识整理,方便后期遇到类似的问题,能够及时查阅使用。

    本节介绍,结构体,结构体属于用户自定义的数据类型,允许用户存储不同的数据类型,包括结构体/结构体定义和使用/结构体数组/结构体指针/ 结构体嵌套结构体/结构体做函数参数/结构体中 const 使用场景/结构体案例以及注意事项。如果有不足之处,欢迎指出,或者你有更好的方法,欢迎留言。

    二、结构体定义和使用

    形式:`struct 结构体名 { 结构体成员列表 };`

    通过结构体创建变量的方式有三种:

    • struct 结构体名 变量名
    • struct 结构体名 变量名 = { 成员1值 , 成员2值...}
    • 定义结构体时顺便创建变量

    说明:

    • 定义结构体时的关键字是struct,不可省略
    • 创建结构体变量时,关键字struct可以省略
    • 结构体变量利用操作符 ''.''  访问成员

    代码:

    1. #include
    2. using namespace std;
    3. //结构体定义
    4. struct student
    5. {
    6. //成员列表
    7. string name; //姓名
    8. int age; //年龄
    9. int score; //分数
    10. }stu3; //结构体变量创建方式3
    11. int main() {
    12. //结构体变量创建方式1
    13. struct student stu1; //struct 关键字可以省略
    14. stu1.name = "张三";
    15. stu1.age = 18;
    16. stu1.score = 100;
    17. cout << "姓名:" << stu1.name << " 年龄:" << stu1.age << " 分数:" << stu1.score << endl;
    18. //结构体变量创建方式2
    19. struct student stu2 = { "李四",19,60 };
    20. cout << "姓名:" << stu2.name << " 年龄:" << stu2.age << " 分数:" << stu2.score << endl;
    21. stu3.name = "王五";
    22. stu3.age = 18;
    23. stu3.score = 80;
    24. cout << "姓名:" << stu3.name << " 年龄:" << stu3.age << " 分数:" << stu3.score << endl;
    25. system("pause");
    26. return 0;
    27. }

    三、结构体数组

    可以将自定义的结构体放入到数组中,以方便维护

    形式:` struct  结构体名 数组名[元素个数] = {  {} , {} , ... {} }`

    代码:

    1. #include
    2. using namespace std;
    3. //结构体定义
    4. struct student
    5. {
    6. //成员列表
    7. string name; //姓名
    8. int age; //年龄
    9. int score; //分数
    10. };
    11. int main() {
    12. //结构体数组
    13. struct student arr[3] =
    14. {
    15. {"张三",18,80 },
    16. {"李四",19,60 },
    17. {"王五",20,70 }
    18. };
    19. for (int i = 0; i < 3; i++)
    20. {
    21. cout << "姓名:" << arr[i].name << " 年龄:" << arr[i].age << " 分数:" << arr[i].score << endl;
    22. }
    23. system("pause");
    24. return 0;
    25. }

    四、 结构体指针

    可以通过指针访问结构体中的成员

    说明:

    • 结构体指针可以通过 -> 操作符 来访问结构体中的成员

    代码:

    1. #include
    2. using namespace std;
    3. //结构体定义
    4. struct student
    5. {
    6. //成员列表
    7. string name; //姓名
    8. int age; //年龄
    9. int score; //分数
    10. };
    11. int main() {
    12. struct student stu = { "张三",18,100, };
    13. struct student* p = &stu;
    14. p->score = 80; //指针通过 -> 操作符可以访问成员
    15. cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
    16. system("pause");
    17. return 0;
    18. }

    五、 结构体嵌套结构体

    结构体中的成员可以是另一个结构体

    说明:

    • 在结构体中可以定义另一个结构体作为成员,用来解决实际问题

    代码:

    1. #include
    2. using namespace std;
    3. //学生结构体定义
    4. struct student
    5. {
    6. //成员列表
    7. string name; //姓名
    8. int age; //年龄
    9. int score; //分数
    10. };
    11. //教师结构体定义
    12. struct teacher
    13. {
    14. //成员列表
    15. int id; //职工编号
    16. string name; //教师姓名
    17. int age; //教师年龄
    18. struct student stu; //子结构体 学生
    19. };
    20. int main() {
    21. struct teacher t1;
    22. t1.id = 10000;
    23. t1.name = "老王";
    24. t1.age = 40;
    25. t1.stu.name = "张三";
    26. t1.stu.age = 18;
    27. t1.stu.score = 100;
    28. cout << "教师 职工编号: " << t1.id << " 姓名: " << t1.name << " 年龄: " << t1.age << endl;
    29. cout << "辅导学员 姓名: " << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分数: " << t1.stu.score << endl;
    30. system("pause");
    31. return 0;
    32. }

    六、结构体做函数参数

    将结构体作为参数向函数中传递

    传递方式有两种:

    • 值传递
    • 地址传递

    说明:

    • 如果不想修改主函数中的数据,用值传递,反之用地址传递

    代码:

    1. #include
    2. using namespace std;
    3. //学生结构体定义
    4. struct student
    5. {
    6. //成员列表
    7. string name; //姓名
    8. int age; //年龄
    9. int score; //分数
    10. };
    11. //值传递
    12. void printStudent(student stu)
    13. {
    14. stu.age = 28;
    15. cout << "子函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
    16. }
    17. //地址传递
    18. void printStudent2(student* stu)
    19. {
    20. stu->age = 28;
    21. cout << "子函数中 姓名:" << stu->name << " 年龄: " << stu->age << " 分数:" << stu->score << endl;
    22. }
    23. int main() {
    24. student stu = { "张三",18,100 };
    25. //值传递
    26. cout << "值传递:" << endl;
    27. printStudent(stu);
    28. cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
    29. cout << endl;
    30. //地址传递
    31. cout << "地址传递:" << endl;
    32. printStudent2(&stu);
    33. cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
    34. system("pause");
    35. return 0;
    36. }

    七、结构体中 const 使用场景

    可以用const来防止误操作

    代码:

    1. #include
    2. using namespace std;
    3. //学生结构体定义
    4. struct student
    5. {
    6. //成员列表
    7. string name; //姓名
    8. int age; //年龄
    9. int score; //分数
    10. };
    11. //const使用场景
    12. void printStudent(const student* stu) //加const防止函数体中的误操作
    13. {
    14. //stu->age = 100; //操作失败,因为加了const修饰
    15. cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl;
    16. }
    17. int main() {
    18. student stu = { "张三",18,100 };
    19. printStudent(&stu);
    20. system("pause");
    21. return 0;
    22. }

    八、结构体案例 (打印出老师数据以及老师所带的学生数据)

    描述:学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下

    • 设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员
    • 学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值
    • 最终打印出老师数据以及老师所带的学生数据。

    代码:

    1. #include
    2. using namespace std;
    3. struct Student
    4. {
    5. string name;
    6. int score;
    7. };
    8. struct Teacher
    9. {
    10. string name;
    11. Student sArray[5];
    12. };
    13. void allocateSpace(Teacher tArray[], int len)
    14. {
    15. string tName = "教师";
    16. string sName = "学生";
    17. string nameSeed = "ABCDE";
    18. for (int i = 0; i < len; i++)
    19. {
    20. tArray[i].name = tName + nameSeed[i];
    21. for (int j = 0; j < 5; j++)
    22. {
    23. tArray[i].sArray[j].name = sName + nameSeed[j];
    24. tArray[i].sArray[j].score = rand() % 61 + 40;
    25. }
    26. }
    27. }
    28. void printTeachers(Teacher tArray[], int len)
    29. {
    30. for (int i = 0; i < len; i++)
    31. {
    32. cout << tArray[i].name << endl;
    33. for (int j = 0; j < 5; j++)
    34. {
    35. cout << "\t姓名:" << tArray[i].sArray[j].name << " 分数:" << tArray[i].sArray[j].score << endl;
    36. }
    37. }
    38. }
    39. int main() {
    40. srand((unsigned int)time(NULL)); //随机数种子 头文件 #include
    41. Teacher tArray[3]; //老师数组
    42. int len = sizeof(tArray) / sizeof(Teacher);
    43. allocateSpace(tArray, len); //创建数据
    44. printTeachers(tArray, len); //打印数据
    45. system("pause");
    46. return 0;
    47. }

    九、结构体案例 (打印出将数组中的英雄按照年龄进行升序冒泡排序后的结果)

    描述:设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。

    五名英雄信息如下:
            {"刘备",23,"男"},
            {"关羽",22,"男"},
            {"张飞",20,"男"},
            {"赵云",21,"男"},
            {"貂蝉",19,"女"},
    代码:

    1. #include
    2. using namespace std;
    3. //英雄结构体
    4. struct hero
    5. {
    6. string name;
    7. int age;
    8. string sex;
    9. };
    10. //冒泡排序
    11. void bubbleSort(hero arr[], int len)
    12. {
    13. for (int i = 0; i < len - 1; i++)
    14. {
    15. for (int j = 0; j < len - 1 - i; j++)
    16. {
    17. if (arr[j].age > arr[j + 1].age)
    18. {
    19. hero temp = arr[j];
    20. arr[j] = arr[j + 1];
    21. arr[j + 1] = temp;
    22. }
    23. }
    24. }
    25. }
    26. //打印数组
    27. void printHeros(hero arr[], int len)
    28. {
    29. for (int i = 0; i < len; i++)
    30. {
    31. cout << "\t姓名: " << arr[i].name << " 性别: " << arr[i].sex << " 年龄: " << arr[i].age << endl;
    32. }
    33. }
    34. int main() {
    35. struct hero arr[5] =
    36. {
    37. {"刘备",23,"男"},
    38. {"关羽",22,"男"},
    39. {"张飞",20,"男"},
    40. {"赵云",21,"男"},
    41. {"貂蝉",19,"女"},
    42. };
    43. int len = sizeof(arr) / sizeof(hero); //获取数组元素个数
    44. cout << "未排序前:" << endl;
    45. printHeros(arr, len); //打印
    46. cout << "排序后:" << endl;
    47. bubbleSort(arr, len); //排序
    48. printHeros(arr, len); //打印
    49. system("pause");
    50. return 0;
    51. }

     

  • 相关阅读:
    SQL注入攻击讲解及PHP防止SQL注入攻击的几种方法
    SpringCloud - 服务调用
    移动端人脸风格化技术的应用
    Go笔记20221124
    手工测试如何进阶自动化测试?熬夜7天整理出这一份3000字的超全学习指南...
    D365: 压缩
    MySQL的卸载与安装
    深度学习入门(十九)深度学习计算——自定义层
    基于JAVA口腔医院网站计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    力扣每日一题43:字符串相乘
  • 原文地址:https://blog.csdn.net/u014361280/article/details/127606307