[C++基础学习-07]----C++结构体详解

前言

结构体(Struct)是C++中一种用户定义的复合数据类型,用于存储不同类型的数据项。结构体可以包含不同类型的数据成员,这些数据成员可以是基本类型(如int、float、char等),也可以是数组、指针、其他结构体等。

正文

01-结构体简介

结构体的基本语法:

cpp 复制代码
struct 结构体名 {
    数据类型 成员1;
    数据类型 成员2;
    // 更多成员
};

// 声明结构体变量
struct 结构体名 变量名;

// 初始化结构体变量
结构体名 变量名 = {初始值1, 初始值2, ...};

02-结构体的定义和使用

具体代码和解释如下:

cpp 复制代码
#include<iostream>
using namespace std;
#include<string>
// 1、创建学生数据类型  :学生包括 (姓名,年龄,分数)
// 自定义的数据类型,一些类型的集合组成的一个类型
// 语法 struct 类型名称(做到见名知意) {成员列表}
struct Student
{
	// 成员列表

	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;

}s3;

// 2、通过类型创建具体学生
// 2.1、struct 结构体名 变量名
// 2.2、struct 结构体名 变量名 ={初始值1,初始值2 .....}
// 2.3、定义结构体时顺便创建变量



int main()
{
	/*  下面三种定义方法中,1和2使用最多
	*/

	// 2.1、struct 结构体名 变量名

	Student s1;  // struct Student s1;这里定义时,C++中可以直接将结构体关键字省略,结果一样
	s1.name = "张三";
	s1.age = 20;
	s1.score = 100;
	cout << "姓名: " << s1.name << " 年龄: " << s1.age << " 分数: " << s1.score << endl;
	// 2.2、struct 结构体名 变量名 ={初始值1,初始值2 .....}
	
	Student s2 = {"李四",19,80};

	cout << "姓名: " << s2.name << " 年龄: " << s2.age << " 分数: " << s2.score << endl;
	
	// 2.3、定义结构体时顺便创建变量

	s3.name = "王五";
	s3.age = 21;
	s3.score = 91;
	cout << "姓名: " << s3.name << " 年龄: " << s3.age << " 分数: " << s3.score << endl;


	system("pause");
	return 0;
}

03-结构体数组

具体代码和解释如下:

cpp 复制代码
#include<iostream>
using namespace std;
#include<string>


// 结构体数组
// 1、定义结构体数组
struct Student
{
	// 成员列表

	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;

};

int main()
{
	
	// 2、创建结构体数组
	struct Student stuArray[3]=
	{
		{"张三",18,100},
		{"李四",20,89},
		{"王五",21,90}
	};
	// 3、给结构体数组中国的某一个进行更换 
	stuArray[2].name = "赵六";  // stuArray[2] 指的是数组的第三个人
	stuArray[2].age = 22;
	stuArray[2].score = 95;

	// 4、遍历结构体数组中的参数  打印输出后可以发现,第三个人的信息已经被更改
	for (int i = 0; i < 3;i++)
	{
		cout << " 姓名: "  << stuArray[i].name 
			 << " 年龄:  " << stuArray[i].age 
			 << " 分数: " << stuArray[i].score << endl;
	}



	system("pause");
	return 0;
}

04-结构体指针

具体代码和解释如下:

cpp 复制代码
#include<iostream>
using namespace std;
#include<string>



// 1、定义学生结构体
struct Student
{
	// 成员列表

	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;

};

int main()
{

	// 2、创建学生结构体变量
	Student s = { "张三",18,100 };

	// 3、通过指针指向结构体变量
	Student *p = &s; //  将出现这种错误,无法从"Student *"转换为"int *",因为&s返回的是一个student数据类型,因此,指针应该定义该类型

	// 4、遍历结构体数组中的参数  打印输出后可以发现,第三个人的信息已经被更改
	cout << "姓名: " << p->name << " 年龄: " << p->age
		<< " 分数: "<<p->score << endl;

	

	system("pause");
	return 0;
}

05-结构体嵌套结构体

具体代码和解释如下:

cpp 复制代码
#include<iostream>
using namespace std;
#include<string>


// 1、定义学生结构体
struct Student
{
	// 成员列表

	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;

};

// 2、定义老师结构体

struct teacher
{ 
	int id;  // 教师编号
	string name;  // 教师姓名
	int age;    // 教师年龄
	Student stu;  // 辅导学生
};


int main()
{

	// 结构体嵌套结构体
	// 创建老师
	teacher t;
	t.name = "王五";
	t.id = 000001;
	t.age = 50;
	t.stu.name = "张三";
	t.stu.age = 20;
	t.stu.score = 100;
	
	cout << "老师姓名: " << t.name << " 老师编号: " << t.id<<" 老师年龄: "<<t.age
		<< " 学生姓名: " << t.stu.name << " 年龄: " << t.stu.age<< " 成绩: " << t.stu.score<< endl;



	system("pause");
	return 0;
}

06-结构体做函数参数

具体代码和解释如下:

cpp 复制代码
#include<iostream>
using namespace std;
#include<string>


// 1、定义学生结构体
struct Student
{
	// 成员列表

	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;

};

// 打印学生信息的函数
// 1、值传递
void printStudent1(Student s)
{
	s.age = 100;
	cout << "子函数1中打印:" << " 姓名: " << s.name << " 年龄: " << s.age << " 成绩: " << s.score << endl;
}
// 2、地址传递
void printStudent2(struct Student *p)
{
	p->age = 100;
	cout << "子函数2中打印:" << " 姓名: " << p->name << " 年龄: " << p->age << " 成绩: " << p->score << endl;
}


int main()
{

	// 结构体做函数参数,将结构体作为参数向函数传递
	// 将学生传入一个参数中,要打印学生的所有信息

	// 创建结构体变量
	Student s;
	s.name = "王五";	
	s.age = 20;
	s.score = 100;

	//	printStudent1(s);
	printStudent2(&s);   // 应该放在打印之前
	cout <<"main函数中打印:"<< " 姓名: " << s.name << " 年龄: " << s.age << " 成绩: " << s.score << endl;
	

	system("pause");
	return 0;
}

07-结构体中const使用场景

具体代码和解释如下:

cpp 复制代码
#include<iostream>
using namespace std;
#include<string>


// 1、定义学生结构体
struct Student
{
	// 成员列表

	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;

};

void printStudent(const Student *p)
{
	// 这里加入const之后,便不可以再对实参进行修改
	cout << "姓名: " << p->name << " 年龄: " << p->age << " 成绩: " << p->score << endl;
}


int main()
{

	// 结构体做函数参数,将结构体作为参数向函数传递
	// 将学生传入一个参数中,要打印学生的所有信息

	// 由于此时只有一个学生,因此觉得打印出来非常简单,但是如果一个学校的人都在里面,
	// 都打印出来,将非常占用内存,因此,定义打印函数时,最佳的方法就是定义一个指针,永远只存有四个字节
	Student s;
	s.name = "王五";
	s.age = 20;
	s.score = 100;

	printStudent(&s);
	

	system("pause");
	return 0;
}

08-结构体案例1

具体代码和解释如下:

cpp 复制代码
#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(Teacher tArray[], int len)
{

	string nameSeed = "ABCDE";
	for (int i = 0; i < len;i++)
	{ 
		
		tArray[i].tName = "Teacher_";
		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 random = rand() % 61 + 40;  // 40~100
			tArray[i].sArray[j].score = random;
		}
	}

}
// 打印老师及学生信息
void printInof(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、创建三名老师的数组
	Teacher tArray[3];

	int len = sizeof(tArray) / sizeof(tArray[0]);

	// 2、通过函数给三名老师的信息赋值,并给老师带的学生信息赋值
	allocateSpace(tArray, len);

	// 3、打印所有老师和学生的信息
	printInof(tArray,len);

	system("pause");
	return 0;
}

09-结构体案例2

具体代码、解释和运行结果如下:

cpp 复制代码
#include <iostream>
using namespace std;
#include <string>
#include <ctime>


// 定义英雄结构体
struct Hero
{
	// 成员列表

	// 姓名
	string hName;
	// 分数
	int age;
	// 性别
	string sex;

};


void printInof1(Hero hArray[], int len)
{
	cout << "排序前: " << endl;

	for (int i = 0; i < len; i++)
	{
		cout <<"姓名: " << hArray[i].hName <<" 年龄: "<<hArray[i].age
			<<" 性别: "<<hArray[i].sex<< endl;

	}

}

void bubbleSort(Hero hArray[], int len)
{
	for (int i = 0; i < len-1;i++)
	{
		for (int j = 0; j < len - i - 1;j++)
		{
			if (hArray[j].age>hArray[j+1].age)
			{
				Hero temp = hArray[j];
				hArray[j] = hArray[j + 1];
				hArray[j + 1] = temp;
			}
		}
	}
}

void printInof2(Hero hArray[], int len)
{
	cout << "排序后: " << endl;

	for (int i = 0; i < len; i++)
	{
		cout << "姓名: " << hArray[i].hName << " 年龄: " << hArray[i].age
			<< " 性别: " << hArray[i].sex << endl;

	}

}

int main()
{
	
	// 1、创建英雄数组
	Hero hArray[5] = {
		{ "刘备",23,"男" },
		{ "关羽",22,"男" },
		{ "张飞",20,"男" },
		{ "赵云",21,"男" },
		{ "貂蝉",19,"女" },
	};
	int len = sizeof(hArray) / sizeof(hArray[0]);
	// 2、打印排序前的英雄
	printInof1(hArray, len);

	// 3、通过函数对英雄信息进行冒泡排序

	bubbleSort(hArray, len);

	// 4、打印排序后的顺序
	printInof2(hArray, len);

	system("pause");
	return 0;
}

总结

结构体可以用来组织和管理多个相关的数据项,方便对它们进行统一处理。在C++中,结构体还可以包含函数成员,这种结构体称为类(Class),用于实现面向对象编程。结构体和类是C++中重要的数据类型,被广泛应用于各种程序设计场景中。

相关推荐
wjs202439 分钟前
SQLite Glob 子句
开发语言
每天题库c3 小时前
水利水电安全员C证考试题库资料,2024年全国考试通用!
学习·考证·水利水电安全员
MarkHD4 小时前
javascript 常见设计模式
开发语言·javascript·设计模式
舞者H4 小时前
源码层面学习动态代理
java·学习
海盗猫鸥4 小时前
C++入门基础篇(1)
开发语言·c++·学习
阳光九叶草LXGZXJ4 小时前
南大通用数据库-Gbase-8a-学习-44-DDLEVENT恢复
linux·数据库·sql·学习
专注成就自我4 小时前
java使用easypoi模版导出word详细步骤
java·开发语言·word
多多*5 小时前
SpringBoot 启动流程六
java·开发语言·spring boot·后端·spring
极乐码农5 小时前
Spring学习03-[Spring容器核心技术IOC学习进阶]
java·学习·spring
让你三行代码QAQ5 小时前
SpringSecurity初始化过程
java·开发语言