C++基础 结构体

1. 概念:用户自定义的数据类型,用于储存不同类型的数据

*一些常用类型集合组成的类型

2. 结构体的定义和使用

a. 结构体定义语法:

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

    //结构体定义
    struct student
    {
        //成员列表
        string name;  //姓名
        int age;      //年龄
        int score;    //分数
    }

b. 结构体变量创建的三种方法:

i. struct 结构体名 变量名

        struct student stu1; //struct 关键字可以省略
        
            stu1.name = "张三";
            stu1.age = 18;
            stu1.score = 100;

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

        struct student stu2 = { "李四",19,60 };

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

        struct student
        {
            //成员列表
            string name;  //姓名
            int age;      //年龄
            int score;    //分数
        }stu3;

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

3. 结构体数组

a. 作用:将自定义的结构体放入数组,方便调用

b. 语法:

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

c. 示例:

    '''
    #include<iostream>
    #include<string>
    using namespace std;
    //结构体数组
    //定义结构体student
    struct student
    {
        string name;
        int age;
        int score;
    };
    
    int main()
    {
    
        //1.创建结构体数组并赋值
        struct student stuarr[3] =
        {
            {"张三",20,80},
            {"李四",39,30},
            {"王五",40,50}
        };
    
        //遍历结构体数组中的元素
        for (int i = 0; i < 3; i++)
        {
            cout << " 姓名 " << stuarr[i].name
                << " 年龄 " << stuarr[i].age
                << " 成绩 " << stuarr[i].score
                << endl;
        }
    
        system("pause");
        return 0;
    }
    '''

4. 结构体指针

a. 通过指针访问结构体中的成员

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

c. 示例:

    '''
    #include<iostream>
    #include<string>
    using namespace std;
    //结构体指针
    //定义结构体student
    struct student
    {
        string name;
        int age;
        int score;
    };
    
    int main()
    {
    
        //1.创建结构体变量并赋值
        struct student stu = { "张三",20,80 };
    
        //2.创建结构体指针
        struct student* p = &stu;
        cout << " 姓名 " << p->name
            << " 年龄 " << p->age
            << " 成绩 " << p->score
            << endl;
    
        system("pause");
        return 0;
    }
    '''

5. 结构体嵌套结构体

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

b. 示例:

    '''
    #include<iostream>
    #include<string>
    using namespace std;
    //结构体嵌套
    //班级结构体中嵌套学生结构体
    //定义结构体student
    struct student
    {
        string name;
        int age;
        int score;
    };
    //定义结构体classroom
    struct classroom
    {
        string classname;
        int area;
        struct student stu;
    };
    
    
    int main()
    {
    
        //1.创建结构体变量1并赋值
        struct classroom room1 = 
        {
            "一班",//教室名
            20,//面积
            {"张三",30,100}//教室结构体中的学生
        };
    
        //2.创建结构体变量2并赋值
        struct classroom room2;
        room2.classname = "二班";
        room2.area = 30;
        room2.stu.name = "李四";
        room2.stu.age = 32;
        room2.stu.score = 83;
    
    
        
        //2.遍历结构体1和2
        cout << " 教室 " << room1.classname
            << " 面积 " << room1.area
            << " 学生名 " << room1.stu.name
            << " 学生年龄 " << room1.stu.age
            << " 学生成绩 " << room1.stu.score
            << endl;
        cout << " 教室 " << room2.classname
            << " 面积 " << room2.area
            << " 学生名 " << room2.stu.name
            << " 学生年龄 " << room2.stu.age
            << " 学生成绩 " << room2.stu.score
            << endl;
    
        system("pause");
        return 0;
    }
    '''

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

6. 结构体做函数参数

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

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

7. 结构体中const的使用

a. 作用:用const来防止误操作

b. 语法:

        返回值类型 函数名(const 结构体名 结构体变量指针)

c. 示例:

    '''
    void printStudent(const student *stu) //加const防止函数体中的误操作
    {
        //stu->age = 100; //操作失败,因为加了const修饰
        cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl;
    
    }
    '''

*在函数参数列表中一般采用地址传递(结构体指针),可以避免复制大量副本(值传递结构体数组回复制所有元素),而使用地址传递只会占用4个字节

8.结构体案例

a.描述:

学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下:
设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员,学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值,最终打印出老师数据以及老师所带的学生数据。

b.示例:

'''
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
//结构体案例1
//学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下:
//设计学生和老师的结构体,其中在老师的结构体中,
//有老师姓名和一个存放5名学生的数组作为成员,
//学生的成员有姓名、考试分数,创建数组存放3名老师,
//通过函数给每个老师及所带的学生赋值,
//最终打印出老师数据以及老师所带的学生数据。


//定义结构体student
struct student
{
    string sname;
    int score;
};

//定义结构体teacher
struct teacher
{
    string tname;
    struct student sArray[5];
};

//给老师和学生赋值
void input(teacher tArray[],int len)
{
    string tTempName = "老师";
    string sTempName = "学生";
    string nameSeed = "ABCDE";
    for (int i = 0; i < len; i++)
    {
        //给老师赋值
        tArray[i].tname = tTempName + nameSeed[i];
        for (int j = 0; j < 5; j++)
        {
            //给学生赋值
            tArray[i].sArray[j].sname = sTempName + nameSeed[j];
            tArray[i].sArray[j].score = rand() % 61 + 40;
        }
    }
}

//输出老师数据和所带学生的数据
void print(struct teacher tArray[], int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << tArray[i].tname << endl;
        for (int j = 0; j < 5; j++)
        {
            cout << "\t姓名 " << tArray[i].sArray[j].sname
                << "成绩" << tArray[i].sArray[j].score << endl;
        }
    }
}

int main()
{
    //随机数种子
    srand((unsigned int)time(NULL));//添加头文件#include<ctime>
    //定义teacher类型结构体数组
    struct teacher tArray[3];
    int len = sizeof(tArray) / sizeof(tArray[0]);

    input(tArray, len);
    print(tArray, len);

    system("pause");
    return 0;
}
'''

注:当时做笔记的时候中英文标点未分,大家见谅

感谢大家的阅读,欢迎大家评论和指出我的不足

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。