• C++学习记录(三)


    8.1 结构体

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

    8.2 结构体定义和使用

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

    • struct 结构体名 变量名
    • struct 结构体名 变量名={成员1值,成员2值}
    • 定义结构体时顺便创建变量
    //自定义的结构体,就是变量的集合
    struct Student
    {
    	string name;
    	int age;
    }
    
    //通过学生类型创建具体学生
    struct Student s1;
    s1.name = 'luna';
    s1.ago = 21;
    
    struct Student s2 = {'luna',21}; //创建时struct可以省略
    
    struct Student
    {
    	string name;
    	int age;
    }s3;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    总结1:定义结构体时关键字是struct,不可省略
    总结2:创建结构体变量时,关键字struct可以省略
    总结3:结构体变量的操作符 s1.name通过 " . "访问成员

    8.3 结构体数组

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

    #include
    using namespace std;
    #include
    //结构体数组
    //1. 定义结构体
    struct Student
    {
    	string name;
    	int age;
    }
    //2.创建结构体数组
    int main(){}
    	struct Student stuArray[3] =
    	{
    		{"zhangsan",18},
    		{"lisi",20},
    		{"wangwu",23}
    	}
    //3.给结构体数组中的元素赋值
    stuArray[2].name = "zhaoliu" //将数组中第二个元素的name修改
    
    //4.遍历结构体数组
    for (int i=0;i<3;i++)
    	{
    	stuArray[i].name;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    8.4 结构体指针

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

    //结构体定义
    struct student
    {
    	//成员列表
    	string name;
    	int age;
    	
    };
    
    int main(){
    //1.创建学生结构体变量
    //2.通过指针指向结构体变量
    //3.通过指针指向结构体变量中的数据
    	struct student s = {"zhangsan", 18, 100};
    	struct student *p = &s;
    	p->name;
    	p->age;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    8.5 结构体嵌套结构体

    作用:结构体中的成员可以是另一个结构体
    例如:每个老师辅导一个学员,一个老师的结构体中还记录一个学生的结构体
    示例:

    //定义学生结构体
    struct student
    {
    	string name;
    	int age;
    	int score;
    };
    
    //定义教师结构体
    struct teacher
    {
    	int id;
    	string name;
    	int age;
    	struct student stu;
    };
    
    int main(){
    	teacher t;
    	t.id = 001;
    	t.name = "teacher wang";
    	t.age = 46;
    	t.stu.name = "zhangsan";
    	t.stu.age = 15;
    	t.stu.score = 80;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    结构体做函数参数

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

    • 值传递
    • 地址传递(形参变化会影响实参)
    struct student
    {
    	string name;
    	int age;
    	int score;
    };
    //值传递
    void printStudent1(struct student s){
    	cout << s.name<<s.age<<s.score<<endl;
    }
    //地址传递
    void printStudent2(struct student *p){
    	cout<< p->name << p->age << p->score <<endl;
    }
    int main(){
    	student s;
    	s.name = "zhangsan";
    	s.age = 28;
    	s.score = 80;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    8.7 结构体中const使用场景

    作用:用const来防止误操作
    示例:

    struct student
    {
    	string name;
    	int age;
    	int score;
    };
    
    //const使用场景
    void printStudent(const student *stu)	//加const防止误操作
    {
    	//stu->age = 20; 这里会赋值失败,只有有读的操作,有const不能写入值
    	cout << stu->name << stu->age << stu->score <<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    8.8 结构体案例

    8.8.1 案例一

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

    struct Student
    {
    	string sName;
    	int score;
    };
    
    struct Teacher
    {
    	string tName;
    	struct Student sArray[5];
    };
    
    //给学生和老师赋值的函数
    void allocateSpace(struct Teacher tArray, int len)
    {
    	for(int i=0;i<len;i++)
    	{
    	tArray[i].tName = "teacher_";
    	tArray[i].tName += i;
    	for (int j=0;j<5;j++)
    	{
    		int random = rand()%61 + 40  //40~100
    		tArray[i].sArray[j].sName = "student_";
    		tArray[i].sArray[j].sName += j;
    		tArray[i].sArray[j].score = 60;
    	}
    	}
    }
    void printInfo(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 << tArray[i].sArray[j].score <<endl;
    		}
    	}
    }
    int main(){
    	//随机数种子 #include 
    	srand(unsigned int)time(NULL);
    	struct Teacher tArray[3];
    
    	int len = sizeof(tArray) / sizeof(tArray[0]);
    	allocateSpace(tArray,len);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    8.8.2 案例二

    案例描述:
    设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。通过冒泡排序的算法,将数组中的英雄按照年龄进行生序排序,最终打印排序后的结果。
    5名英雄信息:
    {“刘备”,23,“男”},
    {“关羽”,22,“男”},
    {“张飞”,20,“男”},
    {“赵云”,21,“男”},
    {“貂蝉”,19,“女”}

    #include
    using namespace std;
    
    struct Hero
    {
    	string name;
    	int age;
    	string sex;
    };
    //冒泡排序,实现年龄生序排序
    void bubbleSort(struct Hero heroArray[], int len)
    {
    	for(int i=0;i<len-1;i++)
    	{
    		for(int j=0;j<len-i-1;j++)
    		{
    			if(heroArray[j].age>heroArray[j+1].age)
    			{
    				struct Hero tmp heroArray[j];
    				heroArray[j] = heroArray[j];
    				heroArray[j+1] = tmp;
    				}
    			}
    		}
    	}
    int main(){
    //创建数组存放5名英雄
    	struct Hero heroArray[5]=
    	{
    		{"刘备"23"男"},
    		{"关羽"22"男"},
    		{"张飞"20"男"},
    		{"赵云"21"男"},
    		{"貂蝉"19"女"},
    	};
    	int len = sizeof(heroArray)/sizeof(heroArray[0]);
    	for(int i=0;i<)
    	{
    		cout << heroArray[i].name <<endl;
    		cout << heroArray[i].age <<endl;
    		cout << heroArray[i].sex <<endl;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    JAVA毕业设计剧院售票系统计算机源码+lw文档+系统+调试部署+数据库
    Abnova CD8 VHH-hIgG1 重组人单克隆抗体方案
    Dubbo基于注解方式的配置
    商城免费搭建之java商城 java电子商务Spring Cloud+Spring Boot+mybatis+MQ+VR全景+b2b2c
    Django之路由分发
    【小笔记】fasttext文本分类问题分析
    Learning Git Branch 题解(基础、高级、Git远程仓库)
    MySQL报错:Duplicate entry ‘xxx‘ for key ‘xxx‘
    阿里巴巴中国站item_search_img按图搜索1688商品(拍立淘) API 返回值说明
    Win11电脑摄像头打开看不见,显示黑屏如何解决?
  • 原文地址:https://blog.csdn.net/qq_41571224/article/details/126152405