C++ 基础篇 8 之结构体

结构体基本概念

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

结构体定义和使用

语法:struct 结构体名 {结构体成员列表};

通过结构体创建变量的方法:struct 结构体名 变量名;struct 结构体名 变量名 = {成员1,成员2…};定义结构体时顺便创建变量

程序举例

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

// 创建学生数据类型
struct Student
{
	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;
} s3;

int main()
{
	// 1. 通过结构体创建结构体变量 然后再赋值
	struct Student s1;
	s1.name = "小苏";
	s1.age = 19;
	s1.score = 100;
	cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;

	// 2. 通过结构体创建变量的同时赋值
	struct Student s2 = { "小柯", 15, 100 };
	cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;


	// 3. 通过定义结构体时创建结构体变量
	s3.name = "小智";
	s3.age = 18;
	s3.score = 100;
	cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;

	system("pause");
	return 0;
}

结构体数组

作用:将自定义的结构体放入到数组中方便维护

语法:struct 结构体名 数组名[元素个数] = {{}, {}…{}}

程序举例

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

// 创建学生数据类型
struct Student
{
	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;
};

int main()
{
	// 创建结构体数组
	struct Student stuArr[4] = {
		{"小苏", 19, 100},
		{"小柯", 15, 100},
		{"小智", 18, 100}
	};

	// 修改结构体数组的值
	stuArr[3].name = "小佳";
	stuArr[3].age = 19;
	stuArr[3].score = 100;

	// 遍历结构体数组
	for (int i = 0; i < sizeof(stuArr) / sizeof(stuArr[0]); i++)
	{
		cout << "姓名:" << stuArr[i].name << " 年龄:" << stuArr[i].age << " 分数:" << stuArr[i].score << endl;
	}

	system("pause");
	return 0;
}

结构体指针

作用:通过指针访问结构体中的成员

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

程序举例

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

// 创建学生数据类型
struct Student
{
	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;
};

int main()
{
	// 创建结构体数组
	struct Student s1 = { "小苏", 19, 100 };

	struct Student * p = &s1; // 结构体指针指向结构体变量

	// 通过结构体指针访问结构体内属性
	cout << "姓名:" << p->name << endl;
	cout << "年龄:" << p->age << endl;
	cout << "分数:" << p->score << endl;

	system("pause");
	return 0;
}

结构体嵌套结构体

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

程序举例

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

// 学生结构体
struct Student
{
	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;
};

// 老师结构体
struct Teacher
{
	// 编号
	int id;
	// 姓名
	string name;
	// 年龄
	int age;
	// 所带的学生
	struct Student s;
};

int main()
{
	// 创建老师
	// struct Teacher t1;
	Teacher t1;
	t1.id = 1;
	t1.name = "刚哥";
	t1.age = 29;
	// 在其内部创建学生
	t1.s.name = "小猪";
	t1.s.age = 19;
	t1.s.score = 100;

	cout << "老师姓名:" << t1.name << " 老师编号:" << t1.id << " 老师年龄:" << t1.age << endl;
	cout << "老师所带学生的姓名:" << t1.s.name << " 老师所带学生的年龄:" << t1.s.age << " 老师所带学生的分数:" << t1.s.score << endl;

	system("pause");
	return 0;
}

结构体作函数参数

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

分类:值传递和地址传递

程序举例

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

// 学生结构体
struct Student
{
	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;
};

// 老师结构体
struct Teacher
{
	// 编号
	int id;
	// 姓名
	string name;
	// 年龄
	int age;
	// 所带的学生
	struct Student s;
};

// 打印信息的函数
void printStudent1(Teacher t1)
{
	t1.age = 39;
	cout << "值传递子函数中:" << endl;
	cout << "老师姓名:" << t1.name << " 老师编号:" << t1.id << " 老师年龄:" << t1.age << endl;
	cout << "老师所带学生的姓名:" << t1.s.name << " 老师所带学生的年龄:" << t1.s.age << " 老师所带学生的分数:" << t1.s.score << endl;
}

// 打印信息的函数
void printStudent2(Teacher *p)
{
	p->age = 39;
	cout << "地址传递子函数中:" << endl;
	cout << "老师姓名:" << p->name << " 老师编号:" << p->id << " 老师年龄:" << p->age << endl;
	cout << "老师所带学生的姓名:" << p->s.name << " 老师所带学生的年龄:" << p->s.age << " 老师所带学生的分数:" << p->s.score << endl;
}

int main()
{
	// 创建老师
	// struct Teacher t1;
	Teacher t1;
	t1.id = 1;
	t1.name = "刚哥";
	t1.age = 29;
	// 在其内部创建学生
	t1.s.name = "小猪";
	t1.s.age = 19;
	t1.s.score = 100;

	// 主函数中打印信息
	cout << "老师姓名:" << t1.name << " 老师编号:" << t1.id << " 老师年龄:" << t1.age << endl;
	cout << "老师所带学生的姓名:" << t1.s.name << " 老师所带学生的年龄:" << t1.s.age << " 老师所带学生的分数:" << t1.s.score << endl;
	cout << endl;

	// 值传递函数打印信息
	printStudent1(t1);
	cout << endl;
	// 值传递后主函数打印信息
	cout << "值传递后主函数打印信息:" << endl;
	cout << "老师姓名:" << t1.name << " 老师编号:" << t1.id << " 老师年龄:" << t1.age << endl;
	cout << "老师所带学生的姓名:" << t1.s.name << " 老师所带学生的年龄:" << t1.s.age << " 老师所带学生的分数:" << t1.s.score << endl;
	cout << endl;
	// 地址传递函数打印信息
	printStudent2(&t1);
	cout << endl;
	// 地址传递后主函数打印信息
	cout << "地址传递后主函数打印信息:" << endl;
	cout << "老师姓名:" << t1.name << " 老师编号:" << t1.id << " 老师年龄:" << t1.age << endl;
	cout << "老师所带学生的姓名:" << t1.s.name << " 老师所带学生的年龄:" << t1.s.age << " 老师所带学生的分数:" << t1.s.score << endl;
	
	system("pause");
	return 0;
}

结构体中 const 使用场景

作用:用 const 防止误操作

程序举例

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

// 学生结构体
struct Student
{
	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;
};

// 老师结构体
struct Teacher
{
	// 编号
	int id;
	// 姓名
	string name;
	// 年龄
	int age;
	// 所带的学生
	struct Student s;
};

void printStudent(const Teacher *p)
{
	// p->age = 39; // 报错
	cout << "地址传递子函数中:" << endl;
	cout << "老师姓名:" << p->name << " 老师编号:" << p->id << " 老师年龄:" << p->age << endl;
	cout << "老师所带学生的姓名:" << p->s.name << " 老师所带学生的年龄:" << p->s.age << " 老师所带学生的分数:" << p->s.score << endl;
}

int main()
{
	// 创建老师
	// struct Teacher t1;
	Teacher t1;
	t1.id = 1;
	t1.name = "刚哥";
	t1.age = 29;
	// 在其内部创建学生
	t1.s.name = "小猪";
	t1.s.age = 19;
	t1.s.score = 100;

	// 地址传递函数打印信息
	printStudent(&t1);

	system("pause");
	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

是我来晚了!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值