C++学习笔记⑧结构体

目录

结构体的定义和使用

结构体数组

结构体指针

结构体嵌套结构体

结构体做函数参数

结构体中const使用场景

结构体案例1

 自己写的版本

 黑马的版本


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

结构体的定义和使用

 

 

 

 

 

 

 第三种使用比较少,不建议使用;

创建完结构体,在使用的时候,struct关键字可以省略👇(定义时不可省略!!!)

 

 


结构体数组

 

 

 

 


结构体指针

 总结:结构体指针可以通过->操作符 来访问结构体中的成员

 


结构体嵌套结构体

 

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


结构体做函数参数

 

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

struct Student
{
	string name;
	int age;
	int score;

};


//打印学生信息函数
//值传递
void printStudent1(struct Student s)
{
	s.age = 100;
	cout << "学生的姓名:" << s.name << " 学生的年龄:" << s.age << " 学生的分数:" << s.score << endl;
}

//打印学生信息函数
//地址传递
void printStudent2(struct Student *p)
{
	p->age = 100;
	cout << "学生的姓名:" << p->name << " 学生的年龄:" << p->age << " 学生的分数:" << p->score << endl;
}

int main()
{

	struct Student s = { "lisa", 20, 99 };

	//printStudent1(s);

	printStudent2(&s);
	cout << "学生的姓名:" << s.name << " main中的学生的年龄:" << s.age << " 学生的分数:" << s.score << endl;

	system("pause");

	return 0;//结束
}

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

 


结构体中const使用场景

作用:用const来防止误操作。

 用指针虽然能节省代码空间,只占4个字节即可,但改变值的时候会使原本的变量跟着改变

因此const就有用武之地了👇

 加入了const之后,一旦有修改的操作就会报错,可以防止我们的误操作。


结构体案例1

 

 自己写的版本

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

struct Student
{
	string name;
	int score;

};

struct Teacher
{
	string name;
	struct Student stu_arr[5];

};

void printmessage(struct Teacher * teacher, string t1_name, string t2_name, string t3_name)
{
	teacher[0].name = t1_name;
	teacher[1].name = t2_name;
	teacher[2].name = t3_name;

	cout << "教师1的姓名:" << teacher[0].name << "  教师2的姓名:" << teacher[1].name << "  教师3的姓名:" << teacher[2].name << endl;

}

void printmessage2(struct Teacher* teacher, string s1_name, string s2_name, string s3_name, string s4_name, string s5_name,
				   int s1_score, int s2_score, int s3_score, int s4_score, int s5_score)
{
	teacher->stu_arr[0].name = s1_name;
	teacher->stu_arr[1].name = s2_name;
	teacher->stu_arr[2].name = s3_name;
	teacher->stu_arr[3].name = s4_name;
	teacher->stu_arr[4].name = s5_name;

	teacher->stu_arr[0].score = s1_score;
	teacher->stu_arr[1].score = s2_score;
	teacher->stu_arr[2].score = s3_score;
	teacher->stu_arr[3].score = s4_score;
	teacher->stu_arr[4].score = s5_score;

}

int main()
{

	struct Teacher tea_arr[3];
	
	printmessage(tea_arr, "Mr.LI", "Mr.ZHANG", "Mr.GAO");

	printmessage2(&tea_arr[0], "张三", "李四", "王五", "刘六", "高七", 90, 80, 10, 100, 20);
	printmessage2(&tea_arr[1], "阿三", "阿四", "阿五", "阿六", "阿七", 70, 60, 70, 30, 40);
	printmessage2(&tea_arr[2], "王三", "何四", "余五", "丁六", "林七", 55, 12, 77, 53, 95);

	cout << "  教师1的学生1的姓名:" << tea_arr[0].stu_arr[0].name << "  他的分数为 " << tea_arr[0].stu_arr[0].score << endl << "  教师1的学生2的姓名:" << tea_arr[0].stu_arr[1].name << "  他的分数为 " << tea_arr[0].stu_arr[1].score << endl
		 << "  教师1的学生3的姓名:" << tea_arr[0].stu_arr[2].name << "  他的分数为 " << tea_arr[0].stu_arr[2].score <<endl << "  教师1的学生4的姓名:" << tea_arr[0].stu_arr[3].name << "  他的分数为 " << tea_arr[0].stu_arr[3].score << endl
		 << "  教师1的学生5的姓名:" << tea_arr[0].stu_arr[4].name << "  他的分数为 " << tea_arr[0].stu_arr[4].score << endl;

	cout << endl;

	cout << "  教师2的学生1的姓名:" << tea_arr[1].stu_arr[0].name << "  他的分数为 " << tea_arr[1].stu_arr[0].score << endl << "  教师2的学生2的姓名:" << tea_arr[1].stu_arr[1].name << "  他的分数为 " << tea_arr[1].stu_arr[1].score << endl
		 << "  教师2的学生3的姓名:" << tea_arr[1].stu_arr[2].name << "  他的分数为 " << tea_arr[1].stu_arr[2].score << endl << "  教师2的学生4的姓名:" << tea_arr[1].stu_arr[3].name << "  他的分数为 " << tea_arr[1].stu_arr[3].score << endl
		 << "  教师2的学生5的姓名:" << tea_arr[1].stu_arr[4].name << "  他的分数为 " << tea_arr[1].stu_arr[4].score << endl;

	cout << endl;

	cout << "  教师3的学生1的姓名:" << tea_arr[2].stu_arr[0].name << "  他的分数为 " << tea_arr[2].stu_arr[0].score << endl << "  教师3的学生2的姓名:" << tea_arr[2].stu_arr[1].name << "  他的分数为 " << tea_arr[2].stu_arr[1].score << endl
		 << "  教师3的学生3的姓名:" << tea_arr[2].stu_arr[2].name << "  他的分数为 " << tea_arr[2].stu_arr[2].score << endl << "  教师3的学生4的姓名:" << tea_arr[2].stu_arr[3].name << "  他的分数为 " << tea_arr[2].stu_arr[3].score << endl
		 << "  教师3的学生5的姓名:" << tea_arr[2].stu_arr[4].name << "  他的分数为 " << tea_arr[2].stu_arr[4].score << endl;

	system("pause");

	return 0;
}

 效果:

 黑马的版本

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

struct Student
{
	string name;
	int score;

};

struct Teacher
{
	string name;
	struct Student stu_arr[5];

};

void allocateSpace(struct Teacher tArray[], int len)
{
	
	string nameSeed = "ABCDE";

	for (int i = 0; i < len; i++)
	{
		tArray[i].name = "Teacher_";
		tArray[i].name += nameSeed[i];

		for (int j = 0; j < 5; j++)
		{
			tArray[i].stu_arr[j].name = "Student_";
			tArray[i].stu_arr[j].name += nameSeed[j];

			tArray[i].stu_arr[i].score = 60;

		}
		

	}
}


void printInfo(struct Teacher tArray[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "老师的姓名:" << tArray[i].name << endl;

		for (int j = 0; j < 5; j++)
		{
			int random = rand() % 101;//0~100
			tArray[i].stu_arr[j].score = random;
			cout << "\t学生的姓名:" << tArray[i].stu_arr[j].name << "  该学生的成绩:" << tArray[i].stu_arr[j].score << endl;
		}


	}
}


int main()
{

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

	struct Teacher tea_arr[3];
	int len = sizeof(tea_arr) / sizeof(tea_arr[0]);

	allocateSpace(tea_arr, len);
	printInfo(tea_arr, len);


	system("pause");

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值