• C++ 结构体


    概念

    结构体属于用户自定义的数据类型,允许用户存储不同的数据类型 

    语法

    struct 结构体名 { 结构体成员列表 }; 

    通过结构体创建变量的方式有三种:struct 关键字可以去掉

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

    1. #include
    2. using namespace std;
    3. // 定义结构体
    4. struct student{
    5. string name;
    6. int age;
    7. string hobby[3];
    8. }stu3; // 实例化结构体方式三 定义结构体时实例化
    9. int main(){
    10. // 实例化结构体方式一:
    11. struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
    12. cout << "姓名:" << stu1.name << " 年龄:" << stu1.age << " 爱好:" << stu1.hobby[0] << "," << stu1.hobby[1] << "," << stu1.hobby[2] << endl;
    13. // 实例化结构体方式二:
    14. struct student stu2;
    15. stu2.name = "hehe";
    16. stu2.age = 19;
    17. stu2.hobby[0] = "c++";
    18. cout << "姓名:" << stu2.name << " 年龄:" << stu2.age << " 爱好:" << stu2.hobby[0] << endl;
    19. stu3.name = "heihei";
    20. stu3.age = 18;
    21. stu3.hobby[0] = "python";
    22. stu3.hobby[1] = "c++";
    23. cout << "姓名:" << stu3.name << " 年龄:" << stu3.age << " 爱好:" << stu3.hobby[0] << "," << stu3.hobby[1] << endl;
    24. cout << sizeof(stu1) << endl; // 136
    25. cout << sizeof(stu2) << endl; // 136
    26. cout << sizeof(stu3) << endl; // 136
    27. cout << sizeof(struct student) << endl; // 136
    28. }

    总结1:定义结构体时的关键字是struct,不可省略

    总结2:结构体定义完成后,所占与的字节数就已经确定

    总结3:结构体通过 ' . ' 访问成员,或为成员变量赋值

    结构体数组

    作用:将自定义的结构体放入到数组中方便维护

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

    1. #include
    2. using namespace std;
    3. // 定义结构体
    4. struct student{
    5. string name;
    6. int age;
    7. string hobby[3];
    8. }
    9. int main(){
    10. // 结构体定义完成后,所占与的字节数就已经确定
    11. cout << sizeof(struct student) << endl;
    12. // 表示数组arr内的元素类型是 student 结构体
    13. student arr[3] = {
    14. {"haha", 20, {"python", "golang", "c++"}},
    15. {"hehe", 19, {"c++"}},
    16. {"heihei", 18, {"python", "c++"}},
    17. };
    18. for (int i = 0; i < sizeof(arr)/ sizeof(student); ++i) {
    19. cout << "姓名: " << arr[i].name << endl;
    20. }
    21. }

    结构体指针

    • 利用操作符  -> 可以通过结构体指针访问结构体属性
    1. #include
    2. using namespace std;
    3. // 定义结构体
    4. struct student{
    5. string name;
    6. int age;
    7. string hobby[3];
    8. };
    9. int main(){
    10. struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
    11. // 结构体指针
    12. struct student * p = &stu1;
    13. cout << p->hobby << endl; // 0x7ffd0edc1458
    14. cout << *p->hobby << endl; // python 解释: *p->hobby 等价于 *(p->hobby) 等价于 p->hobby[0] 等价于 stu1.hobby[0]
    15. cout << p->hobby[0] << endl; // python 等价于 stu1.hobby[0]
    16. cout << p->name << endl; // haha 等价于 stu1.name
    17. cout << p->age << endl; // 20 等价于 stu1.age
    18. }

    结构体嵌套

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

    1. #include
    2. using namespace std;
    3. struct student{
    4. string name;
    5. int age;
    6. string hobby[3];
    7. };
    8. struct teacher{
    9. int id;
    10. string name;
    11. struct student stu;
    12. };
    13. int main(){
    14. // 实例化结构体方式一:
    15. struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
    16. // 结构体嵌套
    17. struct teacher tea;
    18. tea.id = 1;
    19. tea.name = "wang";
    20. tea.stu = stu1;
    21. cout << "teacher id = " << tea.id
    22. << " teacher name= " << tea.name
    23. << " student name= " << tea.stu.name
    24. << " student age= " << tea.stu.age << endl;
    25. }

    结构体做函数参数

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

    传递方式有两种:

    • 值传递
    • 地址传递
    1. #include
    2. using namespace std;
    3. // 定义结构体
    4. struct student{
    5. string name;
    6. int age;
    7. string hobby[3];
    8. };
    9. // 值传递
    10. void printStudent(student stu){
    11. stu.age = 22;
    12. cout << "值传递中打印:" << endl;
    13. cout << "姓名:" << stu.name
    14. << " 年龄:" << stu.age
    15. << " 爱好:" << stu.hobby[0] << "," << stu.hobby[1] << "," << stu.hobby[2] << endl;
    16. }
    17. // 地址传递
    18. void printStudent2(student * stu){
    19. stu->age = 25;
    20. cout << "地址递中打印:" << endl;
    21. cout << "姓名:" << stu->name
    22. << " 年龄:" << stu->age
    23. << " 爱好:" << stu->hobby[0] << "," << stu->hobby[1] << "," << stu->hobby[2] << endl;
    24. }
    25. int main(){
    26. // 实例化结构体方式一:
    27. struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
    28. cout << "初始值:" << endl;
    29. cout << "姓名:" << stu1.name
    30. << " 年龄:" << stu1.age
    31. << " 爱好:" << stu1.hobby[0] << "," << stu1.hobby[1] << "," << stu1.hobby[2] << endl;
    32. printStudent(stu1);
    33. cout << "值传递后打印:" << endl;
    34. cout << "姓名:" << stu1.name
    35. << " 年龄:" << stu1.age
    36. << " 爱好:" << stu1.hobby[0] << "," << stu1.hobby[1] << "," << stu1.hobby[2] << endl;
    37. printStudent2(&stu1);
    38. cout << "地址递后打印:" << endl;
    39. cout << "姓名:" << stu1.name
    40. << " 年龄:" << stu1.age
    41. << " 爱好:" << stu1.hobby[0] << "," << stu1.hobby[1] << "," << stu1.hobby[2] << endl;
    42. }
    43. >>>>:
    44. 初始值:
    45. 姓名:haha 年龄:20 爱好:python,golang,c++
    46. 值传递中打印:
    47. 姓名:haha 年龄:22 爱好:python,golang,c++
    48. 值传递后打印:
    49. 姓名:haha 年龄:20 爱好:python,golang,c++
    50. 地址递中打印:
    51. 姓名:haha 年龄:25 爱好:python,golang,c++
    52. 地址递后打印:
    53. 姓名:haha 年龄:25 爱好:python,golang,c++

    结构体中 const使用场景

    作用:用const来防止误操作

    1. #include
    2. using namespace std;
    3. // 定义结构体
    4. struct student{
    5. string name;
    6. int age;
    7. string hobby[3];
    8. };
    9. // 地址传递, 使用地址传递的意义是为了节省内存
    10. void printStudent2(const student * stu){ // const 修饰了指针 -- 变量指针
    11. stu->age = 25; // 报错 指针指向的值不可以修改
    12. }
    13. int main(){
    14. struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
    15. printStudent2(&stu1);
    16. }

  • 相关阅读:
    初识AOS --------AOS学习笔记系列
    Nature、science、cell旗下刊物
    OpenELA 正式公开 Enterprise Linux 源代码
    [MAUI]用纯C#代码写两个漂亮的时钟
    1.4 输入语句(Python)
    米勒拉宾算法——素性测试
    【Kotlin基础系列】第1章 简介
    PAT甲级:1043 Is It a Binary Search Tree
    SVN创建分支
    数据仓库即服务概述
  • 原文地址:https://blog.csdn.net/Waller_/article/details/126366028