一、结构体基本概念

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

1、结构体定义和使用

语法:struct 结构体名 {结构体成员列表};
通过结构体创建变量的方式有三种:

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

    示例:

    1. struct student
    2. {
    3. string name;
    4. int age;
    5. int score;
    6. };

    示例:

    #include <iostream>
    using namespace std;
    int main()
    {
      struct student
      {
          string name;
          int age;
          int score;
      }s3;
      student s1;
      s1.name = "zs";
      s1.age = 25;
      s1.score = 95;
      cout << "姓名:" << s1.name << "年龄:" << s1.age << "分数:" << s1.score << endl;
      student s2 = {"ls",12,90};
      cout << "姓名:" << s2.name << "年龄:" << s2.age << "分数:" << s2.score << endl;
      s3.name = "ww";
      s3.age = 23;
      s3.score = 80;
      cout << "姓名:" << s3.name << "年龄:" << s3.age << "分数:" << s3.score << endl;
    }
    

    结果:
    **

    二、结构体数组

    作用:将自定义的结构体放入到数组中方便维护
    语法:struct 结构体名 数组名[元素个数]={ { },{ },{ }………. };

    示例:

    #include <iostream>
    using namespace std;
    int main()
    {
      struct student
      {
          string name;
          int age;
          int score;
      };
      student s[3] = {
    
          {"张三",25,90},
          {"李四",23,95},
          {"王五",21,80}
      };
      for(int i=0;i<3;i++)
      {
          cout << "姓名:   " << s[i].name << "年龄:   " << s[i].age << "分数:   " << s[i].score << endl;
      }
    }
    

    结果:
    image.png

    三、结构体指针

    作用:通过指针访问结构体中的成员

  • 利用操作符->可以通过结构体指针访问结构体属性

    示例:

    #include <iostream>
    using namespace std;
    struct student
    {
      string name;
      int age;
      int score;
    };
    int main()
    {
      student s1[] =
      {
          {"张三",25,95},
          {"李四",23,90},
          {"王五",21,85}
      };
      student* p=s1;
      for (int i = 0; i < 3; i++)
      {
          cout << "姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl;
          p++;
      }
    }
    

    image.png
    结果:
    image.png

    四、结构体嵌套结构体

    作用:结构体中的成员可以是另一个结构体
    例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体 ```cpp struct teacher { int id; int age; string name; student stu; };

struct student { int age; string name; int score; };

<a name="FWq66"></a>
### 示例:
```cpp
#include <iostream>
using namespace std;
struct student
{
    int age;
    string name;
    int score;
};
struct teacher
{
    int id;
    int age;
    string name;
    student stu;      //结构体中嵌套一个学生的结构体
};
int main()
{
    teacher t[2] =
    {
        {202101,35,"张三丰",21,"张三",95},
        {202101,35,"张三丰",23,"李四",85}
    };
    teacher* p = t;

    for (int i = 0; i < 2; i++)
    {
        cout << p->age<<"    "<< p->id<<"    " << p->name<<"    " << p->stu.name<<endl;
        p++;
    }
}

结果:
image.png
或者用另外一种方式也可以,结果也是一样的

#include <iostream>
using namespace std;
struct student
{
    int age;
    string name;
    int score;
};
struct teacher
{
    int id;
    int age;
    string name;
    student stu;
};
int main()
{
    student stu[2] =
    {
        {21,"张三",95},
        {23,"李四",85}
    };
    teacher t[2] =
    {
        {202101,35,"张三丰",stu[0]},
        {202101,35,"张三丰",stu[1]}
    };
    teacher* p = t;

    for (int i = 0; i < 2; i++)
    {
        cout << p->age<<"    "<< p->id<<"    " << p->name<<"    " << p->stu.name<<endl;
        p++;
    }
}

结果:
image.png

五、结构体做函数参数

作用:将结构体作为参数向函数中传递
传递方式有两种

  • 值传递
  • 地址传递

    示例1:值传递

    #include <iostream>
    using namespace std;
    struct student
    {
      string name;
      int age;
      int score;
    }stu;
    //值传递
    void printzhi(student s)
    {
      s.age = 100;
      cout << "在值传递子函数中 学生姓名:" << s.name << "  学生年龄:" << s.age << "  学生分数:" << s.score << endl;
    }
    int main()
    {
      stu.name = "张三";
      stu.age = 20;
      stu.score = 95;
    
      //值传递
      printzhi(stu);
      //在main函数中的打印效果
      cout << "在main函数中 学生姓名:" << stu.name << "  学生年龄:" << stu.age << "  学生分数:" << stu.score << endl;
    }
    

    结果:
    image.png

    示例2:地址传递

    ```cpp

    include

    using namespace std; struct student { string name; int age; int score; }stu;

//地址传递 void printdizhi(student* s) { s->age = 100; cout << “在地址传递子函数中 学生姓名:” << s->name << “ 学生年龄:” << s->age << “ 学生分数:” << s->score << endl; } int main() { stu.name = “张三”; stu.age = 20; stu.score = 95;

//地址传递
printdizhi(&stu);
//在main函数中的打印效果
cout << "在main函数中 学生姓名:" << stu.name << "  学生年龄:" << stu.age << "  学生分数:" << stu.score << endl;

}

**结果:**<br />**![image.png](https://cdn.nlark.com/yuque/0/2021/png/2352229/1614230263079-dd796a32-e83f-4fc2-85bd-860718d2addb.png#align=left&display=inline&height=79&margin=%5Bobject%20Object%5D&name=image.png&originHeight=158&originWidth=953&size=29968&status=done&style=none&width=476.5)**<br />**注意:**可以看到,以值传递的形式,形参未能改变实参的值,而地址传递的形式,形参改变了实参的值
<a name="JDhFa"></a>
# 六、结构体中const的使用场景
**作用:**用<br />const来防止误操作<br />**示例:**<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/2352229/1614235221117-ceed5a3b-ca91-4ce4-a052-cbfa30906f78.png#align=left&display=inline&height=163&margin=%5Bobject%20Object%5D&name=image.png&originHeight=326&originWidth=913&size=34294&status=done&style=none&width=456.5)
<a name="nKk46"></a>
# 七、游戏案例
**案例描述:**_设计一个英雄的结构体,包括成员姓名、性别、年龄,创建结构体数组,数组中存放5名英雄,通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。_<br />_五名英雄信息如下:_<br />_{"刘备",33"男"},_<br />_{"关于",22"男"},_<br />_{"张飞",20"男"},_<br />_{"赵云",21"男"},_<br />_{"貂蝉",19"女"},_<br />**示例:**
```cpp
#include <iostream>
using namespace std;
struct Hero
{
    string name;
    int age;
    char sex;
};
void Print_Hero(Hero* hero,int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << "\t英雄名字:"<<hero->name<<" 年龄: "<<hero->age<<" 性别: "<<hero->sex<<endl;
        hero++;
    }
}
void MPPX(Hero hero[],int len)
{
    for (int i = 0; i < len-1; i++)
    {
        for (int j = 0; j < len - i -1; j++)
        {
            if (hero[j].age > hero[j+1].age)
            {
                Hero temp;
                temp.age = hero[j].age;
                hero[j].age = hero[j+1].age;
                hero[j+1].age = temp.age;
            }
        }

    }
    Print_Hero(hero, len);
}
int main()
{
    Hero hero[5] =
    {
        {"刘备",33,'男'},
        {"关于",22,'男'},
        {"张飞",20,'男'},
        {"赵云",21,'男'},
        {"貂蝉",19,'女'}
    };
    int len = sizeof(hero) / sizeof(hero[0]);
    Hero *p = hero;
    cout << "排序前的英雄:" << endl;
    Print_Hero(p, len);
    cout << "排序后的英雄:" << endl;
    MPPX(hero,len);
    return 0;
}

结果:
image.png
**