结构体

发布时间 2023-11-08 00:34:46作者: 取我方天画戟来

结构体

  • 结构体基本的概念

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

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

      • // 1、 创建学生数据类型 : 学生包括(姓名、年龄、分数)
        struct Student
        {
            string names;
            int ages;
            int score;
        };
        

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

      • struct 结构体名 变量名

        • // 2.1 stuct Student s1
              struct Student s1;
              // 给s1 属性赋值,通过 “  .  ” 访问结构体变量中的属性
              s1.names="张三";
              s1.ages=18;
              s1.score=100;
          
      • struct 结构体名 变量名={成员1值,成员2值...}​​

        • struct Student s2 ={"李四",19,100};​​
      • 定义结构体时顺便创建变量

        • // 2.3 在定义结构体时顺便创建结构体变量
          struct Students
          {
              string names;
              int ages;
              int score;
          }s3;
          
      • struct 关键字可以省略,但是结构体定义不可以省略

  • 结构体数组

    • 作用: 将自定义的结构体放到数组中方便维护
    • struct 结构体名 数组名[元素个数] ={{},{},{}。。。{}}​​
    • #include <iostream>
      using  namespace  std;
      

      struct Student
      {
      string names;
      int ages;
      int score;
      };

      int main()
      {
      struct Student arr[3]={
      {"张三",18,100},
      {"张三",19,100},
      {"张三",20,100}
      };
      // 给结构体元素赋值
      arr[3].names="李四";

      }

  • 结构体指针

    • 通过指针访问结构中的成员

      • 使用操作符“->”可以通过结构体指针访问结构体属性
      • #include <iostream>
        using  namespace  std;
        

        struct Student
        {
        string names;
        int ages;
        int score;
        };

        int main()
        {
        // 1. 创建学生结构体变量
        struct Student s1={"张三",18,100};
        // 2. 通过指针指向结构体变量
        Student *p1= &s1;
        // 3. 通过指针访问结构体变量中的数据
        p1->ages=20;
        cout<<p1->ages<<endl;
        }

  • 结构体嵌套结构体

    • 作用: 结构体中的成员可以是另外一个结构体

      • #include <iostream>
        using  namespace  std;
        

        struct Student
        {
        string names;
        int ages;
        int score;
        };
        struct Teacher
        {
        int id;
        string names;
        int age;
        struct Student stu;
        };

        int main()
        {
        Teacher t ={10,"老王",20};
        t.stu.ages=10;
        t.stu.names="张三";
        t.stu.score=100;
        }

    • 例如: 每个老师辅导学院,一个老师的结构体中,记录一个学生的结构体

  • 结构体做函数参数

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

    • 传递方式有两种:

      • 值传递
      • 地址传递
    • #include <iostream>
      using  namespace  std;
      #include <string>
      struct Student
      {
          string names;
          int ages;
          int score;
      };
      struct Teacher
      {
          int id;
          string names;
          int age;
          struct Student stu;
      };
      // 1、 值传递
      void printStudent(struct Student s1)
      {
          cout<<s1.names<<"->"<<s1.score<<"->"<<s1.ages;
      }
      // 2、 地址传递
      void printStudent1(struct Student *p1)
      {
          cout<<p1->names<<"_>"<<p1->score<<"_>"<<p1->ages;
      }
      int main()
      {
          // 结构体做函数参数
          // 将学生传入到一个参数中,打印学生身上的所有信息
          // 创建结构体变量
          struct Student s = {"张三",20,85};
          //寮犱笁_>85_>20
          printStudent1(&s);
      

      }

  • 结构体const使用场景

    • 作用: 用const来防止误操作

      • image