【C++】结构体

本文详细介绍了C++中结构体的基础概念,包括结构体的定义与使用、结构体数组、结构体指针、结构体作为函数参数以及const关键字的使用场景。最后提供了一个练习案例,展示了如何创建和操作结构体数组和指针。
摘要由CSDN通过智能技术生成

1.结构体

1.1 结构体的定义和使用

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

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

#include<string>
#include<iostream>
using namespace std;
//1.创建学生数据类型,学生包括(姓名,年龄,分数)
//自定义数据类型,一些类型集合组成一个类型
//语法 struct类型名称{成员列表}
struct Student
{
	//成员列表
	//姓名
	string name;
	//年龄
	int age;
	//分数
	int score;
}s3;//顺便创建结构体变量 


int main()
{
	//2.通过学生类型创建具体学生
    //2.1 struct Student s1        struct可以省略
	struct Student s1;
	//给s1属性赋值,通过.访问结构体变量中的属性
	s1.name = "张三";
	s1.age = 18;
	s1.score = 100;
	cout << " 姓名: " << s1.name << " 年龄: " 
	<< s1.age << " 分数: " << s1.score << endl;
	
	//2.2 struct Student s2 = {...}
	struct Student s2 = { "李四",19,80 };
	cout << " 姓名: " << s2.name << " 年龄: " 
	<< s2.age << " 分数: " << s2.score << endl;

	//2.3在定义结构体时顺便创建结构体变量(不太推荐)
	s3.name = "王五";
	s3.age = 20;
	s3.score = 60;
	cout << " 姓名: " << s3.name << " 年龄: " 
	<< s3.age << " 分数: " << s3.score << endl;
	system("pause");
	return 0;
}

1.2 结构体数组

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

//1.定义结构体   2.创建结构体数组  3.给结构体数组中的元素赋值  4.遍历结构体数组
struct Student
{
	string name;//姓名
	int age;//年龄
	int score;//分数
};

int main()
{
	//结构体数组
	struct Student stuArray[3]=
	{ 
		{"张三",19,100},
		{"李四",20,90},
		{"王五",20,85} 
	};
	stuArray[2].name = "赵六";
	stuArray[2].age = 21;
	stuArray[2].score = 99;
	for (int i = 0; i < 3; i++)
	{
		cout << "姓名:  " << stuArray[i].name
			<< "年龄: " << stuArray[i].age
			<< "分数: " << stuArray[i].score << endl;
	}
	system("pause");
	return 0;
}

1.3 结构体指针

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

#include<iostream>
#include<string>
using namespace std;

struct Student
	{
		string name;//姓名
		int age;//年龄
		int score;//分数
	};

int main()
{
	//创建学生结构体变量
	struct Student s = {"张三",18,100};
	//通过指针指向结构体变量   结构体类型的指针变量
	Student* p = &s;
	//通过指针访问结构体变量中的数据
	cout << "姓名: " << p->name << "年龄: " << p->age 
	<< "分数: " << p->score << endl;
	system("pause");
	return 0; 
}

1.4 结构体做函数参数

//将结构体作为参数向函数中传递
//传递方式: 1.值传递 2.地址传递

1.5 结构体中const的使用场景

用const来防止误操作

#include<iostream>
#include<string>
using namespace std;

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

//const使用场景     使用指针,只需要传输4个字节
void printfStudent(const Student* stu)  //加const防止函数体中的误操作
{
	//stu->age = 100;      //操作失败,因为加了const修饰
	cout << "姓名: " << stu->name << "年龄: " << stu->age 
	<< "分数: " << stu->score << endl;
}

int main()
{
	Student stu = { "张三",19,99 };
	printfStudent(&stu);
	system("pause");
	return 0;
}

1.6 练习案例

在这里插入图片描述

#include<iostream>
#include<string>
#include<ctime>
using namespace std;

//学生的结构体定义
struct Student
{
	//姓名
	string sName;
	//年龄
	int age;
	//成绩
	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 = "Teacher_";
		tArray[i].tName += nameseed[i];//小tips,可以直接在后面加字母

		//通过循环给每名老师所带的学生赋值
		for (int j = 0; j < 5; j++)
		{
			tArray[i].sArray[j].sName = "Student_";
			tArray[i].sArray[j].sName += nameseed[j];

			int random = rand() % 60+40;
			tArray[i].sArray[j].score = random;
		}
	}
}

//打印所有信息
void printfInfo(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].age << "学生成绩: " << tArray[i].sArray[j].score << endl;
		}
	}
}
int main()
{
	//随机数种子,保证每次成绩刷新
	srand((unsigned int)time(NULL));
	//创建3名老师的数组
	struct Teacher tArray[3];
	//通过函数给3名老师的信息赋值,并给每个老师带的学生信息赋值
	int len = sizeof(tArray) / sizeof(tArray[0]);
	allocateSpace(tArray, len);
	//打印所有老师及所带的学生信息
	printfInfo(tArray, len);
	system("pause");
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵远疴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值