结构体详解

9.1 结构体基本概念

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

9.2 结构体定义和使用

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

1. struct 结构体名  变量名

2. struct 结构体名 变量名 = { 成员1值 , 成员2值...}

3. 定义结构体时顺便创建变量

#include <iostream>

using namespace std;

//结构体语法

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

//typedef

struct Student

{

    int ID;

    string name;

    int age;

    int Cscore;

};

typedef struct Student sS;

int main0902()

{

    //结构体变量的创建方式1

    //struct 结构体名 变量名

    sS stu1;

    stu1.ID = 1;

    stu1.name = "张三";

    stu1.age = 18;

    stu1.Cscore = 100;

    cout << "学号:" << stu1.ID << endl << "姓名:" << stu1.name << endl <<

         "年龄:" << stu1.age << endl << "C语言成绩:" << stu1.Cscore << endl;

    //结构体变量的创建方式2

    //struct 结构体名 变量名 = { 赋值 }

    sS stu2 = { 2,"李四",19,80 };

    cout << "学号:" << stu2.ID << endl << "姓名:" << stu2.name << endl <<

         "年龄:" << stu2.age << endl << "C语言成绩:" << stu2.Cscore << endl;

    return EXIT_SUCCESS;

}

总结:

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

2. 在C++中,创建结构体变量时,关键字struct可以省略

3. 结构体变量利用操作符 ''.''  访问成员

9.3 typedef

typedef是为一种数据类型(基本类型或自定义数据类型)定义一个新名字,不能创建新类型。

与#define不同,typedef仅限于数据类型,而不是能是表达式或具体的值

#define发生在预处理,typedef发生在编译阶段

9.4 结构体数组

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

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

#include <iostream>

using namespace std;

struct Student

{

    int ID;

    string name;

    int age;

    int Cscore;

};

int main0904()

{

    int arr[] = { 1,2,3 };

    //struct Student stu[3];

    struct Student stu[3] =

    {

         {1,"张三",18,90},

         {2,"李四",19,60},

         {3,"王五",17,80}

    };

    for (int i = 0; i < sizeof(stu) / sizeof(stu[0]); i++)

    {

         cout << "学号:" << stu[i].ID << endl << "姓名:" << stu[i].name << endl <<

             "年龄:" << stu[i].age << endl << "C语言成绩:" << stu[i].Cscore << endl
<< endl;

    }

    return EXIT_SUCCESS;

}

 

9.5 结构体指针

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

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

#include <iostream>

using namespace std;

struct Student

{

    int ID;

    string name;

    int age;

    int Cscore;

};

int main0905()

{

    struct Student stu = { 1,"张三",19,90 };

    struct Student* p = &stu;

    stu.age = 18;

    p->Cscore = 100; //指针通过 -> 操作符访问成员

    cout << "学号:" << p->ID << endl << "姓名:" << p->name << endl <<

         "年龄:" << p->age << endl << "C语言成绩:" << p->Cscore << endl << endl;

    return EXIT_SUCCESS;

}

 

9.6 结构体嵌套结构体

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

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

#include <iostream>

using namespace std;

struct Student

{

    string name;

    int age;

    int score;

};

struct Teacher

{

    int id;

    string name;

    int age;

    struct Student stu;

    //struct Student stu[100];

};

int main()

{

    struct Teacher t1;

    t1.id = 1;

    t1.name = "老王";

    t1.age = 40;

    t1.stu.name = "张三";

    t1.stu.age = 19;

    t1.stu.score = 80;

    cout << "教师编号:" << t1.id << endl << "教师姓名:" << t1.name << endl <<

         "教师的年龄:" << t1.age << endl << "教师的学生的姓名:" << t1.stu.name << endl

         << "教师的学生的年龄:" << t1.stu.age << endl << "教师的学生的成绩" << t1.stu.score <<
endl;

    return EXIT_SUCCESS;

}

 

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

9.7 结构体做函数参数

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

1. 值传递

2. 地址传递

#include <iostream>

using namespace std;

struct Student

{

    string name;

    int age;

    int score;

};

void printStu(struct Student stu)

{

    stu.age = 18;

    cout << "值传递" << stu.name << endl << stu.age << endl << stu.score << endl;

}

void printStu1(struct Student *p)

{

    p->age = 30;

    cout << "地址传递" << p->name << endl << p->age << endl << p->score << endl;

}

int main()

{

    struct Student stu = { "张三", 19, 80 };

    //值传递

    printStu(stu);

    cout << "主" << stu.name << endl << stu.age << endl << stu.score << endl;

   

    //地址传递

    printStu1(&stu);

    cout << "值传递后的主" << stu.name << endl << stu.age << endl << stu.score <<
endl;

    return EXIT_SUCCESS;

}

 

总结:

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

9.8 结构体中 const使用场景

作用:用const来防止误操作

#include<iostream>

using namespace std;

//学生结构体定义

struct student

{

    //成员列表

    string name;  //姓名

    int age;      //年龄

    int score;    //分数

};

//const使用场景

void printStudent(const student* stu) //加const防止函数体中的误操作

{

//stu->age = 100; //操作失败,因为加了const修饰

cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score <<
endl;

}

int main()

{

    student stu = { "张三",18,100 };

    printStudent(&stu);

    return 0;

}

9.9 结构体案例

案例描述:

1.   设计一个英雄的结构体,包括成员姓名,年龄,性别;

2.   创建结构体数组,数组中存放5名英雄。

3. 通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。

#include <iostream>

using namespace std;

//1、设计一个英雄的结构体,包括成员姓名,年龄,性别;

struct hero

{

    string name;

    int age;

    string sex;

};

void bubbleSort(hero* arr, int len)

{

    for (int i = 0; i < len - 1; i++)

    {

         for (int j = 0; j < len - 1 - i; j++)

         {

             if (arr[j].age > arr[j + 1].age)

             {

                  hero temp = arr[j];

                  arr[j] = arr[j + 1];

                  arr[j + 1] = temp;

             }

         }

    }

}

void printHeros(hero* arr, int len)

{

    for (int i = 0; i < len; i++)

    {

         cout << "姓名:" << arr[i].name << " 性别:" << arr[i].sex << " 年龄:"

             << arr[i].age << endl;

    }

}

int main0908()

{

    //2、创建结构体数组,数组中存放5名英雄。

    struct hero arr[5] =

    {

         {"刘备",23,"男"},

         {"关羽",22,"男"},

         {"张飞",20,"男"},

         {"赵云",21,"男"},

         {"貂蝉",19,"女"},

    };

    //3、通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序

    bubbleSort(arr, sizeof(arr) / sizeof(arr[0]));

    //4、最终打印排序后的结果。

    printHeros(arr, sizeof(arr) / sizeof(arr[0]));

    return EXIT_SUCCESS;

}

 

实践时间(15分钟)

1. 根据案例需求,用C或C++语言编写代码,完成程序。

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

1. 设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员

2. 学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值

3. 最终打印出老师数据以及老师所带的学生数据。

参考代码:

#include <iostream>

#include <ctime>

using namespace std;

//2、设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员

struct Student

{

    string name;

    int score;

};

struct Teacher

{

    string name;

    struct Student sArr[5];

};

void Init(Teacher* tArr, int len)

{

    string tName = "教师";

    string sName = "学生";

    char nameSeed[] = "ABCDE";

    for (int i = 0; i < len; i++)

    {

         tArr[i].name = tName + nameSeed[i];

         for (int j = 0; j < 5; j++)

         {

             tArr[i].sArr[j].name = sName + nameSeed[j];

             tArr[i].sArr[j].score = rand() % 61 + 40; //40~100

         }

    }

}

void print(Teacher* tArr, int len)

{

    for (int i = 0; i < len; i++)

    {

         cout << tArr[i].name << endl;

         for (int j = 0; j < 5; j++)

         {

             cout << "\t学生姓名:" << tArr[i].sArr[j].name << "  分数:" << tArr
[i].sArr[j].score<< endl;

         }

    }

}

int main()

{

    srand((unsigned int)time(NULL));

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

    //3、学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值

    Teacher tArr[3];

    Init(tArr, sizeof(tArr) / sizeof(tArr[0]));

    //4、最终打印出老师数据以及老师所带的学生数据。

    print(tArr, sizeof(tArr) / sizeof(tArr[0]));

    return EXIT_SUCCESS;

}

 

我的代码:

#include<iostream>

#include <ctime>

using namespace std;

struct student

{

    string name;

    int score;

};

struct teacher

{

    string name;

    struct student sarr[5];

};

void init(teacher* tarr,int len)

{

    for (int i = 0; i < len; i++)

    {

         cout << "请输入第"<<i+1<<"位老师的姓名," << endl;

         cin >> tarr[i].name;

         for (int j = 0; j < 5; j++)

         {

             cout << "请输入第" << i + 1 << "位老师的" << "第"<<j+1<<"位学生的姓名和成绩"<<endl<<
endl;

             cin >> tarr[i].sarr[j].name >> tarr[i].sarr[j].score;

         }

    }

}

void print(teacher* tarr,int len)

{

    for (int i = 0; i < len; i++)

    {

         cout <<"第" << i + 1<<"老师的姓名是" << tarr[i].name << endl;

         for (int j = 0; j < 5; j++)

         {

             cout <<"他的第"<<j+1<<"位学生的姓名为" << tarr[i].sarr[j].name <<"成绩为" <<
tarr[i].sarr[j].score << endl;;

         }

    }

   

}

int main()

{

    teacher tarr[3];

    init(tarr, 3);

    print(tarr, 3);

    return 0;

}

 

节约时间所有全部填的1,有兴趣的同学可以自己一个个输入

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信