【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;
}
相关推荐
1白天的黑夜120 小时前
栈-844.比较含退格的字符串-力扣(LeetCode)
c++·leetcode·
重生之我是Java开发战士20 小时前
【数据结构】Java集合框架:List与ArrayList
java·数据结构·list
爱干饭的boy21 小时前
手写Spring底层机制的实现【初始化IOC容器+依赖注入+BeanPostProcesson机制+AOP】
java·数据结构·后端·算法·spring
VBA633721 小时前
如何学习VBA:换一种思路思考问题,利用数据库实现数据处理自动化
开发语言
0_0梅伊阁诗人21 小时前
Django ORM 模型
开发语言·数据库·笔记·python·oracle·django
躲在云朵里`21 小时前
Redis深度解析:核心数据结构、线程模型与高频面试题
数据结构·数据库·redis
林夕忆梦_猫21 小时前
初识C++
开发语言·c++
chxin1401621 小时前
openCV3.0 C++ 学习笔记补充(自用 代码+注释)---持续更新 四(91-)
c++·opencv·计算机视觉
lightqjx21 小时前
【C++】string类 模拟实现
java·开发语言·c++
青草地溪水旁21 小时前
C/C++ 标准库中的 `strspn` 函数
c语言·c++