c++(上)8 结构体

c++(上)

8 结构体
8.1 结构体基本概念

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

8.2 结构体定义和使用

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

通过结构体创建变量的方式有三种:

  • struct 结构体名 变量名
  • struct 结构体名 变量名 = { 成员1值 , 成员2值…}
  • 定义结构体时顺便创建变量
#include<iostream> 
using namespace std;
//结构体定义 struct 关键字不可省略
struct student{
	//成员列表
	string name; //姓名 
	int age;	//年龄
	int score;	//分数
}stu3; //结构体变量创建方式3
int main() {
	//结构体变量创建方式1
	struct student stu1; //struct 关键字可以省略
	stu1.name = "张三"; //结构体变量利用操作符 ‘.’  访问成员
    stu1.age = 18;
	stu1.score = 100;
	cout << "姓名:" << stu1.name << " 年龄:" << stu1.age << " 分数:" << stu1.score << endl;
	//结构体变量创建方式2
	struct student stu2 = { "李四",19,60 };
	cout << "姓名:" << stu2.name << " 年龄:" << stu2.age << " 分数:" << stu2.score << endl;
	stu3.name = "王五"; 
	stu3.age = 18;
	stu3.score = 80;
	cout << "姓名:" << stu3.name << " 年龄:" << stu3.age << " 分数:" << stu3.score << endl;
    system("pause");
	return 0;
}
8.3 结构体数组

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

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

#include<iostream> 
using namespace std;
struct student {
	string name; //姓名 
	int age;	//年龄
	int score;	//分数
};
int main() {
	//结构体数组
	struct student arr[3] ={
		{"张三",18,80 },
		{"李四",19,60 },
		{"王五",20,70 }
	};
	for (int i = 0; i < 3; i++){
		cout << "姓名:" << arr[i].name << " 年龄:" << arr[i].age << " 分数:" << arr[i].score << endl;
	}
    system("pause");
	return 0;
}
8.4 结构体指针

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

  • 利用操作符 -> 可以通过结构体指针访问结构体属性
#include<iostream> 
using namespace std;
struct student {
	string name; //姓名 
	int age;	//年龄
	int score;	//分数
};
int main() {
	 struct student stu = { "张三",18,100, }; 
	 struct student * p = &stu;
	p->score = 80; //指针通过 -> 操作符可以访问成员
	cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
	system("pause");
	return 0;
}
8.5 结构体嵌套结构体

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

例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体

#include<iostream> 
using namespace std;
//学生结构体定义 
struct student{
	//成员列表
	string name; //姓名 
	int age;	//年龄
	int score;	//分数
};
//教师结构体定义 
struct teacher{
	//成员列表
	int id; //职工编号 
	string name; //教师姓名 
	int age;	//教师年龄
	struct student stu; //子结构体 学生
};
int main() {
	struct teacher t1; 
	t1.id = 10000;
	t1.name = "老王";
	t1.age = 40;

	t1.stu.name = "张三"; 
	t1.stu.age = 18;
	t1.stu.score = 100;
	
	cout << "教师 职工编号:" << t1.id << " 姓名:" << t1.name << " 年龄:" << t1.age << endl;
	cout << "辅导学员 姓名:" << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分数:" << t1.stu.score << endl;
	system("pause");
	return 0;
}
8.6 结构体做函数参数

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

传递方式有两种:

  • 值传递
  • 地址传递
#include<iostream> 
using namespace std;
//学生结构体定义 
struct student{
	//成员列表
	string name; //姓名 
	int age;	//年龄
	int score;	//分数
};
//值传递
void printStudent(student stu){
	stu.age = 28;
	cout << "子函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
}
//地址传递
void printStudent2(student* stu)
{
	stu->age = 28;
	cout << "子函数中 姓名:" << stu->name << " 年龄: " << stu->age << " 分数:" << stu->score << endl;
}
int main() {
	student stu = { "张三",18,100 };
	//值传递 
	printStudent(stu);
	cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
	cout << endl;
	//地址传递 
	printStudent2(&stu);
	cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
	system("pause");
	return 0;
}

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

8.7 结构体中 const使用场景

作用:用const来防止误操作

#include<iostream> 
using namespace std;
//学生结构体定义 
struct student{
	//成员列表
	string name; //姓名 
	int age;	//年龄
	int score;	//分数
};
//const使用场景
void printStudent(const student* stu) //加const防止函数体中的误操作
{
	//stu->age = 100; //操作失败,因为加了const修饰
	cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu -> score << endl;
}
int main() {
	student stu = { "张三",18,100 }; 
	printStudent(&stu);
	system("pause");
	return 0;
}
8.8 结构体案例
8.8.1 案例1

案例描述

学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下:

设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值,最终打印出老师数据以及老师所带的学生数据。

#include<iostream> 
using namespace std;
struct Student{
	string name; 
	int score;
};
struct Teacher{
	string name;
	Student sArray[5];
};
void allocateSpace(Teacher tArray[], int len){
	string tName = "教师";
	string sName = "学生"; 
	string nameSeed = "ABCDE";
	for (int i = 0; i < len; i++){
		tArray[i].name = tName + nameSeed[i];
		for (int j = 0; j < 5; j++){
			tArray[i].sArray[j].name = sName + nameSeed[j]; 
			tArray[i].sArray[j].score = rand() % 61 + 40;
		}
	}
}
void printTeachers(Teacher tArray[], int len){
	for (int i = 0; i < len; i++){
		cout << tArray[i].name << endl; 
		for (int j = 0; j < 5; j++){
			cout << "\t姓名:" << tArray[i].sArray[j].name << " 分数:" << tArray[i].sArray[j].score << endl;
		}
	}
}
int main() {
	srand((unsigned int)time(NULL)); //随机数种子 
	Teacher tArray[3]; //老师数组
	int len = sizeof(tArray) / sizeof(Teacher); 
	allocateSpace(tArray, len); //创建数据 
	printTeachers(tArray, len); //打印数据 
	system("pause");
	return 0;
}
8.8.2 案例2

案例描述

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

#include<iostream> 
using namespace std;
//英雄结构体 
struct hero{
	string name; 
	int age; 
	string sex;
};
//冒泡排序
void bubbleSort(hero arr[], int len){
	for (int i = 0; i < len - 1; i++){
		for (int j = 0; j < len - 1 - i; j++){
			if (arr[j].age > arr[j + 1].age){
				hero temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp;
			}
		}
	}
}
//打印数组
void printHeros(hero arr[], int len){
	for (int i = 0; i < len; i++){
		cout << "姓名: " << arr[i].name << " 性别: " << arr[i].sex << " 年龄: "<< arr[i].age << endl;
	}
}
int main() {
	struct hero arr[5] ={
	{"刘备",23,"男"},
	{"关羽",22,"男"},
	{"张飞",20,"男"},
	{"赵云",21,"男"},
	{"貂蝉",19,"女"},
	};
	int len = sizeof(arr) / sizeof(hero); //获取数组元素个数 
	bubbleSort(arr, len); //排序
	printHeros(arr, len); //打印 
	system("pause");
	return 0;
}
<think>嗯,用户这次明确询问C++中嵌套结构体的具体使用方法和示例。看来他正在学习或工作中需要处理复杂数据结构。从提供的引用来看,用户应该是个C++初学者,可能正在尝试理解如何组织多层数据。用户似乎更关注实际应用而非理论,这点从要求"示例"的措辞可以看出。四个引用中[2]和[4]的师生关系示例最贴近日常逻辑,[1]和[3]则涉及指针和动态内存等进阶用法,需要分层说明。考虑到教学效果,应该从简单到复杂逐步推进:先用静态嵌套展示基本语法,再讲解包含指针的动态结构。特别要注意[1]中提到的内存管理问题,这在示例中必须明确标注,避免用户忽略资源释放。用户可能隐含的需求是:如何选择普通嵌套还是指针嵌套?这需要补充对比表格说明。另外[3]提到的"变长数据存储"是嵌套结构的典型应用场景,但原示例不够完整,可以补充链表实现方案。代码演示需要严格遵循引用中的正确写法,特别是指针初始化和构造函数部分。师生示例可复用引用[4],但内存分配示例必须增加delete操作匹配引用[1]的写法——用户提供的引用[1]析构函数处理是正确的,但部分初学者容易遗漏。</think> 好的,下面是关于C++嵌套结构体使用方法的详细说明和示例: ### C++结构体嵌套结构体使用方法 在C++中,结构体嵌套结构体是一种常见的数据组织方式,主要用于建立更复杂、具有层次关系的数据模型。核心概念是**一个结构体(`struct`)的成员变量可以是另一个结构体类型**。 --- #### 基本定义语法 ```cpp // 定义内部结构体 struct InnerStruct { // 成员变量... }; // 定义外部结构体 struct OuterStruct { // 其他成员... InnerStruct innerVar; // 嵌套声明 // 使用指针形式(动态内存) InnerStruct* innerPtr; }; ``` --- #### 使用示例(栈内存) 使用结构体变量直接嵌套(非指针): ```cpp #include <iostream> #include <string> using namespace std; struct Address { string city; string street; int number; }; struct Person { string name; int age; Address addr; // 嵌套结构体变量 }; int main() { Person p; p.name = "张三"; p.age = 30; p.addr.city = "北京"; p.addr.street = "科技园路"; p.addr.number = 101; cout << "姓名: " << p.name << "\n地址: " << p.addr.city << p.addr.street << p.addr.number << endl; return 0; } ``` > **说明**:`Person`直接包含`Address`变量,内存连续分配[^2][^4]。 --- #### 动态内存示例(堆内存) 当嵌套结构大小不确定时,建议使用指针配合动态内存: ```cpp #include <iostream> using namespace std; struct NodeData { int id; string info; }; struct TreeNode { NodeData data; // 值类型嵌套 TreeNode* left; // 指针嵌套(递归结构) TreeNode* right; }; int main() { TreeNode* root = new TreeNode; root->data.id = 1; root->data.info = "根节点"; root->left = new TreeNode{{2, "左子节点"}, nullptr, nullptr}; // C++11聚合初始化 root->right = nullptr; // 释放内存(实际需递归删除) delete root->left; delete root; return 0; } ``` > **关键点**:指针嵌套需手动管理内存,特别是对于树、链表等递归结构[^1][^3]。 --- #### 包含指针成员的结构体 ```cpp struct Project { string name; int priority; }; struct Team { string teamName; Project* mainProject; // 指向外部结构体 }; int main() { Project prj {"AI引擎", 1}; Team team {"研发组", &prj}; // 取地址关联 cout << team.mainProject->name; // 输出: AI引擎 return 0; } ``` --- #### 嵌套结构体的初始化 1. **逐成员初始化** ```cpp Person p = {"李四", 40, {"上海", "南京路", 200}}; ``` 2. **C++11聚合初始化** ```cpp Person p { .name = "王五", .age = 28, .addr = {.city = "广州", .street = "珠江新城", .number = 300} }; ``` --- #### 使用建议 | 场景 | 推荐方式 | 优点 | 缺点 | |------|----------|------|------| | 固定大小 | 直接嵌套(`InnerStruct mem`) | 内存连续,访问快 | 结构体较大时占用栈空间 | | 大小可变 | 指针嵌套(`InnerStruct* ptr`) | 灵活,节省内存 | 需手动管理内存[^1] | | 关联独立对象 | 外部指针(`OuterStruct* link`) | 可复用已有对象 | 需注意对象生命周期 | > **内存管理警示**:当嵌套结构体包含指针时(如`innerPtr`),务必在外部结构体的构造函数中初始化指针(`= nullptr`),在析构函数中释放内存(`delete innerPtr`)[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值