0
点赞
收藏
分享

微信扫一扫

c++(08-结构体)

夏木之下 2022-03-17 阅读 119

文章目录

8 结构体

在这里插入图片描述

8.1 结构体的基本概念

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

8.2 结构体定义和使用

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

  • strcut 结构体名 变量名
  • strcut 结构体名 变量名={成员1,成员2···}
  • 定义结构体时顺便创建变量
#include<iostream>
using namespace std;
#include<string>
//创建学生数据类型,学生包括(姓名,年龄,分数)
//自定义数据类型,一些类型集合组成的数据的一个类型
//语法 strcut 类型名称{成员列表}
struct student
{
	//成员列表
	//姓名
	string name;
	//年龄
	int age;
	//分数
	int score;
}s3;//- 定义结构体时顺便创建变量
//通过学生类型创建具体学生
//- strcut 结构体名 变量名
//-strcut 结构体名 变量名 = { 成员1,成员2··· }
int main()
{
	//struct student s1;
	//给sl属性赋值,通过 . 访问结构体变量中的属性
	struct student s1;//结构体struct可以不写
	s1.name = "张三";
	s1.age = 18;
	s1.score = 100;
	cout << "姓名:" << s1.name <<" " << s1.age<<" " << s1.score << endl;
	struct student s2 = { "李四",18,90 };
	cout << "姓名:" << s2.name << " " << s2.age << " " << s2.score << endl;
	s3.name = "xx";
	s3.age = 15;
	s3.score = 96;
	cout << "姓名:" << s3.name << " " << s3.age << " " << s3.score << endl;
}

8.3 结构体数组

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

//}
#include<iostream>
using namespace std;
#include<string>
//结构体数组
//1.定义结构体
struct student
{
	//name
	string name;
	//age
	int age;
	//score
	int score;
};

int main()
{
	//2.创建结构体数组
	struct student stdArray[3] =
	{
		{"aa",18,100},
		{"bb",15,96},
		{"cc",18,98},
	};
	//3.给结构体数组的元素赋值
	stdArray[2].name = "nn";
	stdArray[2].age = 80;
	stdArray[2].score = 100;
	//4.遍历结构体数组
	for (int i = 0; i <= 2; i++)
	{
		cout << "name: " << stdArray[i].name
			<< "age:  " << stdArray[i].age
			<< "score:  " << stdArray[i].score << endl;;
	}

8.4 结构体指针

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

#include<iostream>
using namespace std;
#include<string>
#include<ctime>
#include "swap.h"
struct student
{
	//name
	string name;
	//age
	int age;
	//score
	int score;
};
//3利用指针访问结构体中的数据
int main()
{
	//1创建学生结构体变量
	struct student s = {"张三",18,80};
	//2创建指针指向结构体变量
	struct student* p = &s;
	//通过结构体指针,访问结构体中的属性,需要利用->
	cout<<p->name<<" ";
	cout<<p->age<<" ";
	cout<<p->score<<" ";
}

8.5 结构体嵌套结构体

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

#include<iostream>
#include<string>
using namespace std;
struct student {
	string name;
	int age;
	int score;
};
struct teacher {
	string name;
	int age;
	int score;
	struct student stu;
};

int main()
{
	teacher t = { "老王",35,100 };
	t.stu.name = "xx";
	t.stu.age = 18;
	t.stu.score = 86;
	cout << t.name << " " << t.age << " " << t.score << endl;
	cout << t.stu.name<<" "<<t.stu.age<<" "<<t.stu.score;
}

8.6 结构体做函数参数

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

  • 值传递
#include<iostream>
#include<string>
using namespace std;
struct student {
	string name;
	int age;
	int score;
};
void printstudent1(struct student s)
{
	cout << "子函数中 姓名:"<<s.name<<"年龄:" << s.age <<"分数:" << s.score;
}
int main()
{
	student s = {"张三",15,90};
	printstudent1(s);
}
  • 地址传递
#include<iostream>
#include<string>
using namespace std;
struct student {
	string name;
	int age;
	int score;
};
void printstudent1(struct student s)
{
	s.age = 100;
	cout << "子函数1中 姓名:" << s.name << "年龄:" << s.age << "分数:" << s.score << endl;;
}
void printstudent2(struct student *p)
{
	cout << "子函数2中 姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
}
int main()
{
	student s = {"张三",15,90};
	cout<< "主函数中 姓名:" << s.name << "年龄:" << s.age << "分数:" << s.score << endl;
	printstudent1(s);
	printstudent2(&s);
}

8.7 结构体中const的使用场景

作用:用const来防止误操作

#include<iostream>
#include<string>
using namespace std;
struct student {
	string name;
	int age;
	int score;
};
//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
void printstudent(const student *s)
{
	//s->age=100;加入const之后,一但有修改操作就会报错,可以防止我们的误操作
	cout << s->name << s->age << s->score << endl;
}
int main()
{
	 struct student s = {"ss",15,100};
	printstudent(&s);
	return 0;
}

在这里插入图片描述

8.8结构体案例

8.8.1案例1

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

#include<iostream>
using namespace std;
#include<string>
#include<ctime>
struct student {
	string sname;
	int score;
};
struct teacher {
	string tname;
	struct student sArray[5];
};
void allocatespace(struct teacher tArray[], int len)
{
	string nameseed = "ABCDE";
	//给老师赋值
	for (int i = 0; i < len; i++)
	{
		tArray[i].tname = "traacher_";
		tArray[i].tname += nameseed[i];
		//通过循环给每名老师所带的学生赋值
		for (int j = 0; j < 5; j++)
		{
			tArray[i].sArray[j].sname = "student_";
			tArray[i].sArray[j].sname += nameseed[j];
			int randow = rand() % 61 + 40;
			tArray[i].sArray[j].score = randow;
		}
		
	}
}
//打印所有信息
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 << "\t学生姓名:" << tArray[i].sArray[j].sname <<
				" 考试分数:"<< tArray[i].sArray[j].score << endl;
		}
	}
}
int main()
{
	//随机数种子
	srand((unsigned int)time(NULL));
	//1创建三名老师的数组
	struct teacher tArray[3 ];
	//2通过函数给3名老师的信息赋值,并给老师带的学生信息赋值
	int len = sizeof(tArray) / sizeof(tArray[0]);
	allocatespace(tArray,len);
	//3打印所有老师及所带的学生信息
	printinfo(tArray,len);
	return 0;
}

老师姓名:traacher_A
        学生姓名:student_A 考试分数:96
        学生姓名:student_B 考试分数:41
        学生姓名:student_C 考试分数:41
        学生姓名:student_D 考试分数:85
        学生姓名:student_E 考试分数:60
老师姓名:traacher_B
        学生姓名:student_A 考试分数:87
        学生姓名:student_B 考试分数:99
        学生姓名:student_C 考试分数:55
        学生姓名:student_D 考试分数:72
        学生姓名:student_E 考试分数:85
老师姓名:traacher_C
        学生姓名:student_A 考试分数:55
        学生姓名:student_B 考试分数:91
        学生姓名:student_C 考试分数:64
        学生姓名:student_D 考试分数:51
        学生姓名:student_E 考试分数:61

C:\Users\HP\Desktop\01 hello world\x64\Debug\01 hello world.exe (进程 7456)已退出,代码为 0。
按任意键关闭此窗口. . .

在这里插入图片描述

8.8.2案例2

案例描述:设计一个英雄的结构体,包括成员姓名,年龄,性别:创建结构体数组,数组中存放5名英雄,通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果

#include<iostream>
#include<string>
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 temp = heroArray[j];
				heroArray[j] = heroArray[j + 1];
				heroArray[j+1] = temp;
			}
		}
	}
};
void printhero(struct hero heroArray[], int len)
{
	cout<<"排序后: "<< endl;
	for (int i = 0; i < len; i++)
	{
		cout << "姓名:" << heroArray[i].name
			<< " 年龄:" << heroArray[i].age
			<< " 性别" << heroArray[i].sex << endl;
	}
};
int main()
{
	struct hero heroArray[5] =
	{
		{"吕布",18,"男"},
		{"貂蝉",20,"女"},
		{"曹操",23,"男"},
		{"西施",25,"女"},
		{"关羽",28,"男"},
	};

	int len = sizeof(heroArray) / sizeof(heroArray[0]);
	cout << "排序前:" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << "姓名:" << heroArray[i].name
			<< " 年龄:" << heroArray[i].age
			<< " 性别" << heroArray[i].sex << endl;
	}

	bubblesort(heroArray, len);

	printhero(heroArray, len);
}

在这里插入图片描述
在这里插入图片描述

举报

相关推荐

08:结构体

C++结构体

C++ 结构体

C++: 结构体

C++ 结构体 struct

0 条评论