C++基础入门学习笔记之【结构体】(七)

结构体

1、结构体的基本概念

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

2、结构体定义和使用

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

在这里插入图片描述


在这里插入图片描述

#include<iostream>
using namespace std;
#include <string> //使用string字符

//1、创建学生数据类型:学生包括(姓名、年龄、分数)
//自定义数据类型,一些类型集合组成的一个类型
struct Student 
{
	//成员列表

	//姓名
	string name;
	//年龄
	int age;
	//分数
	int score;
}s3;//顺便创建结构体变量

//2、通过学生类型创建具体学生
int main() {
	//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 s1={...};
	struct Student s2 = { "李四", 20, 80 };
	cout << s2.name << "---" << s2.age << "---" << s2.score << endl;
	//2.3 在定义结构体时顺便创建结构体变量
	s3.name = "王五";
	s3.age = 28;
	s3.score = 10;
	cout << s3.name << "---" << s3.age << "---" << s3.score << endl;

	system("pause");
	return 0;
}

输出:

张三---18---100
李四---20---80
王五---28---10
3、结构体数组

作用:将自定义的结构体放到数组中方便维护
语法:struct 结构体名 数组名[元素个数]={{}, {}, …{}}

#include<iostream>
using namespace std;
#include <string> //使用string字符

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

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

//2、通过学生类型创建具体学生
int main() {
	//2、创建结构体数组
	struct Student stuArray[3]
	{
		{"zs",18,100},
		{"ls", 20, 50 },
		{"ww",22,80}
	};
	cout << stuArray << endl;
	//3、给结构体数组中的元素赋值
	stuArray[2].name = "zl";
	//4、遍历结构体数组
	for (int i = 0; i < 3; i++)
	{
		cout << "姓名:" << stuArray[i].name
			 << "年龄:" << stuArray[i].age
			 << "分数:" << stuArray[i].score << endl;
	}
	system("pause");
	return 0;
}

输出:

00EFFC8C
姓名:zs年龄:18分数:100
姓名:ls年龄:20分数:50
姓名:zl年龄:22分数:80
4、结构体指针

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

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

#include<iostream>
using namespace std;
#include <string> //使用string字符

//结构体指针
//1、定义结构体
struct Student
{
	//成员列表

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

int main() {
	//1、创建学生结构体变量 struct可以省略
	struct Student s = { "李四", 20, 80 };
	//2、通过指针指向结构体变量
	Student* p = &s;
	//3、通过指针访问结构体变量中的数据
	cout << "p:" << p << endl;
	//通过结构体指针访问结构体属性,需要使用 -> 符号
	cout << "name:" << p->name << "age:" << p->age << "score:" << p->score << endl;
	system("pause");
	return 0;
}

输出:

p:00B7F728
name:李四age:20score:80
5、结构体嵌套结构体

作用:结构体中的成员可以是另一个结构体
在结构体中可以定义另一个结构体作为成员,用来解决实际问题。

例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
在这里插入图片描述

#include<iostream>
using namespace std;
#include <string> //使用string字符

//结构体嵌套结构体
// 学生的结构体
struct Student
{
	//成员列表

	//姓名
	string name;
	//年龄
	int age;
	//分数
	int score;
};
//老师结构体
struct Teacher
{
	//成员列表

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

int main() {
	//创建老师
	Teacher t;
	t.id = 10000;
	t.name = "ww";
	t.age = 30;
	t.stu.name = "xw";
	t.stu.age = 18;
	t.stu.score = 90;
	
	cout << "teacher name:" << t.name << " student name:" << t.stu.name << endl;

	system("pause");
	return 0;
}

输出:

teacher name:ww student name:xw
6、结构体做函数参数

作用:将结构体作为参数向函数中传递
传递方式有两种:
1、值传递
2、地址传递

#include<iostream>
using namespace std;
#include <string> //使用string字符

// 学生的结构体
struct Student
{
	//成员列表

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

//打印学生信息的函数
//1 值传递
void printStudent1(struct Student s)
{
	s.age = 100;
	cout << "子函数1中的姓名:" << s.name << " age:" << s.age << " score:" << s.score << endl;
}

//2 地址传递
void  printStudent2(struct Student* s)
{
	s->age = 200;
	cout << "子函数2中的姓名:" << s->name << " age:" << s->age << " score:" << s->score << endl;
}

int main() {
	//结构体做函数参数
	//将学生传入到一个参数中,打印学生身上的所有信息
	
	//创建结构体变量
	struct Student s1;
	s1.name = "张三";
	s1.age = 18;
	s1.score = 100;

	printStudent1(s1);

	printStudent2(&s1);

	cout << "main函数:" << s1.age << endl;

	system("pause");
	return 0;
}

输出:

子函数1中的姓名:张三 age:100 score:100
子函数2中的姓名:张三 age:200 score:100
main函数:200

在这里插入图片描述

7、结构体中const使用场景

作用:用const防止误操作

#include<iostream>
using namespace std;
#include <string> //使用string字符

//const的使用场景
struct Student
{
	//成员列表

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

//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本
void printStudent1(const Student *s)
{
	//s->150;//加const之后,就不可以修改,防止我们的误操作
	cout << "姓名:" << s->name << " age:" << s->age << " score:" << s->score << endl;
}

int main() {
	//创建结构体变量
	struct Student s1;
	s1.name = "张三";
	s1.age = 18;
	s1.score = 100;

	printStudent1(&s1);
	cout << "main函数的年龄:" << s1.age << endl;
	
	system("pause");
	return 0;
}

输出:

姓名:张三 age:18 score:100
main函数的年龄:18
8、结构体的案例
8.1 案例一

在这里插入图片描述
示意图:
在这里插入图片描述

#include<iostream>
using namespace std;
#include <string> //使用string字符
#include<ctime>

// 学生的结构体
struct Student
{
	//姓名
	string sName;
	//分数
	int score;
};

// 老师的结构体定义
struct teacher
{
	//姓名
	string tName;
	//学生的数组
	struct Student sArray[5];
};

//给老师和学生赋值的函数
void allocateSpace(struct teacher tArray[], int len)
{
	string name_seed = "ABCDE";
	//开始给老师赋值
	for (int i = 0; i < len;i++)
	{
		tArray[i].tName = "teacher_";
		tArray[i].tName +=name_seed[i];

		//通过循环给每个老师带的学生进行赋值
		for (int j = 0; j < 5; j++)
		{
			tArray[i].sArray[j].sName = "student_";
			tArray[i].sArray[j].sName += name_seed[j];
			//int random=rand()%60;//0-59的随机值;
			int random = rand() % 60 + 40;//40-99的随机值
			tArray[i].sArray[j].score = random;
		}
	}
}

//打印所有信息
void printInfo(struct teacher tArray[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "teacher name:" << tArray[i].tName << endl;
		for (int j = 0; j < 5; j++)
		{
			cout << "\tstudent name:" << tArray[i].sArray[j].sName << " student score:" << tArray[i].sArray[j].score << endl;
		}
	}
}

int main() {
	//随机数种子
	srand((unsigned int)time(NULL));
	//1、创建3名老师的数组
	struct teacher tArray[3];
	//2、通过函数给3名老师的信息赋值,并给老师带的学生赋值
	int len = sizeof(tArray) / sizeof(tArray[0]);
	allocateSpace(tArray, len);
	//3、打印所有老师和带的学生的信息
	printInfo(tArray, len);
	system("pause");
	return 0;
}

输出:

teacher name:teacher_A
        student name:student_A student score:95
        student name:student_B student score:97
        student name:student_C student score:87
        student name:student_D student score:89
        student name:student_E student score:67
teacher name:teacher_B
        student name:student_A student score:81
        student name:student_B student score:51
        student name:student_C student score:53
        student name:student_D student score:58
        student name:student_E student score:62
teacher name:teacher_C
        student name:student_A student score:96
        student name:student_B student score:90
        student name:student_C student score:71
        student name:student_D student score:83
        student name:student_E student score:53
8.2 案例二

在这里插入图片描述

#include<iostream>
using namespace std;
#include <string> //使用string字符

//1、设计英雄结构体
//英雄的结构体
struct Hero
{
	//姓名
	string name;
	//年龄
	int age;
	//性别
	string sex;
};

//冒泡排序
void bubbleSort(struct 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)
			{
				struct Hero temp = hArray[j];
				hArray[j] = hArray[j + 1];
				hArray[j + 1] = temp;
			}
		}
	}
}

//打印输出
void printArray(struct Hero hArray[], int len)
{
	for (int i = 0; i < len; i++)
	{
		//疑问:为什么不能这样打印呢:cout<<hArray[i] <<endl;
		cout <<"name:" <<hArray[i].name <<" age:"<< hArray[i].age<<" sex:"<< hArray[i].sex << endl;
	}
}

int main() {
	//2、创建数组存放5名英雄
	struct Hero hArray[5]
	{
		{"刘备", 23, "男"},
		{"关羽", 22, "男"},
		{"张飞", 21, "男"},
		{"赵云", 18, "男"},
		{"曹操", 28, "男"},
	};
	int len = sizeof(hArray) / sizeof(hArray[0]);
	//3、对数组进行排序,按年龄进行升序
	bubbleSort(hArray, len);
	//4、打印输出
	printArray(hArray, len);
	system("pause");
	return 0;
}

输出:

name:赵云 age:18 sex:男
name:张飞 age:21 sex:男
name:关羽 age:22 sex:男
name:刘备 age:23 sex:男
name:曹操 age:28 sex:男
请按任意键继续. . .
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值