【C++】结构体

文章目录

  • 1.结构体
    • [1.1 结构体的定义和使用](#1.1 结构体的定义和使用)
    • [1.2 结构体数组](#1.2 结构体数组)
    • [1.3 结构体指针](#1.3 结构体指针)
    • [1.4 结构体做函数参数](#1.4 结构体做函数参数)
    • [1.5 结构体中const的使用场景](#1.5 结构体中const的使用场景)
    • [1.6 练习案例](#1.6 练习案例)

1.结构体

1.1 结构体的定义和使用

结构体基本概念:属于用户自定义的数据类型,允许用户存储不同的数据类型
struct 结构体名{结构体成员列表};

//struct 结构体名 变量名

//struct 结构体名 变量名={成员1值,成员2值...}

//定义结构体时顺便创建变量

cpp 复制代码
#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 结构体名 数组名[元素个数] = {{},{},{},...}

cpp 复制代码
//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 结构体指针

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

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

cpp 复制代码
#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来防止误操作

cpp 复制代码
#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 练习案例

cpp 复制代码
#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;
}
相关推荐
代码雕刻家20 分钟前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
吾爱星辰1 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer1 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
IT良1 小时前
c#增删改查 (数据操作的基础)
开发语言·c#
小飞猪Jay2 小时前
C++面试速通宝典——13
jvm·c++·面试
Kalika0-02 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
_.Switch2 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
代码雕刻家2 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
一个闪现必杀技2 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
Fan_web2 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery