C++学习笔记8-结构体

8.1 结构体基本概念

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

8.2 结构体定义和使用

语法:

struct 结构体名 {结构体成员列表}; 
访问变量:变量.属性

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

  • struct 结构体名 变量名
  • struct 结构体名 变量名={成员1值,成员2值…}
  • 定义结构体时顺便创建变量
#include<iostream>
#include<string>
using namespace std;

//1.创建学生数据类型:学生包括(姓名,年龄,分数)
//自定义数据类型,一些类型集合组成的一个类型
//语法: struct 类型名称 {成员列表}
struct Student
{
	//成员列表
	//姓名
	string name;
	//年龄
	int age;
	//分数
	int score;
}s3;//顺便创建变量,可以创建多变量:s1,s2,s3;

//2.通过学生类型创建具体学生
int main()
{	
	//2.1 struct 结构体名 变量名 :struct Student s1;
	struct Student s1;  //struct可以省略
	//Student s1;
	//给s1属性赋值,通过.访问结构体变量中的属性;
	s1.name = "张三";
	s1.age = 18;
	s1.score = 100;
	cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;

	//2.2 struct 结构体名 变量名 = { 成员1值,成员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;
}

注意:定义结构体时的关键字struct不可省略,但是创建结构体变量时,关键字struct可以省略。

8.3 结构体数组

作用:将自定义的结构体放入到数组中方便维护,其实就之前的结构体变量存到数组里。
语法

struct 结构体名 数组名[元素个数] = {{},{},{},...{}}

示例:

#include<iostream>
#include<string>
using namespace std;

//定义结构体
struct Student
{
	string name;
	int age;
	int score;
};

int main()
{
	//创建结构体数组
	struct Student stuArray[8] =
	{
		{"张三",18,100},
		{"李四",20,90},
		{"王五",19,80}
	};
	//给结构体数组中的元素赋值
	stuArray[2].name = "赵六";
	stuArray[2].age = 50;

	//遍历结构体数组
	for (int i = 0; i < 5; i++) 
	{
		cout << "  姓名:" << stuArray[i].name
			 << "  年龄:" << stuArray[i].age
			 << "  分数:" << stuArray[i].score << endl;
	//数组中未初始化/赋值的元素为空/0 。
	}
	system("pause");
	return 0;
}

8.4 结构体指针

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

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

示例:

#include<iostream>
#include<string>
using namespace std;
struct Student
{
	string name;
	int age;
	int score;
};

int main()
{
	//创建结构体变量
	Student s = { "张三",18,88 };

	//通过指针指向结构体变量
	Student* p = &s;     //struct Student* p = &s 的 struct 可以省略
	
	//通过指针访问结构体变量中的数据
	cout << "姓名" << p->name << endl;
	cout << "年龄" << p->age << endl;
	cout << "分数" << p->score << endl;
	

	system("pause");
	return 0;
}

8.5结构体嵌套结构体

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

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

示例:

#include<iostream>
#include<string>
using namespace std;

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 = 100;
	t.name = "老王";
	t.age = 50;
	t.stu.name = "小王";
	t.stu.age = 20;
	t.stu.score = 60;

	cout << "老师姓名:" << t.name << " 老师编号:" << t.id << " 老师年龄:" << t.age << " 老师辅导的学生姓名:"
		<< t.stu.name << " 学生年龄:" << t.stu.age << " 学生考试分数为:" << t.stu.score << endl;
	system("pause");
	return 0;
}

8.6 结构体做函数参数

作用:将结构体作为参数向函数中传递。
传递方式有两种:

  • 值传递,不可修改结构体变量的数据。
  • 地址传递,可以修改结构体变量的数据。

示例:

#include<iostream>
#include<string>
using namespace std;

struct student
{
	string name;
	int age;
	int score;
};
//值传递
void printStudent1(student s) //struct student s
{
	s.name = "赵四";
	cout <<"值传递函数中打印\n 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;
}
//地址传递
void printStudent2(student* p) //struct student* p
{
	p->name = "赵四";
	cout << "地址传递函数中打印\n 姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
}

int main()
{
	student s;
	s.name = "小王";
	s.age = 20;
	s.score = 90;

	cout << "main函数中打印\n 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;
	printStudent1(s);
	cout << "现在姓名为:" << s.name << endl;
	printStudent2(&s);
	cout << "现在姓名为:" << s.name << endl;
	system("pause");
	return 0;
}

8.7 结构体中const使用场景

作用:用const来防止误操作。
示例:

#include<iostream>
#include<string>
using namespace std;

struct student
{
	string name;
	int age;
	int score;
};

//使用地址传递(只占4/8个字符),可以减少内存空间
//常量指针,也可以使用student* const p/const student* const p
void printStudent(const student* s) 
{	
	//s->age = 100; //加入const后,一旦有修改的操作就会报错,可以防止我们的误操作。
	cout << "姓名:" << s->name << " 年龄:" << s->age << " 分数:" << s->score << endl;
	student s2 = { "老王",50,60 };
	s = &s2;  //还是可以修改指向的
	cout << "姓名:" << s->name << " 年龄:" << s->age << " 分数:" << s->score << endl;
}

int main()
{	
	student s,s2;
	s.name = "小王";
	s.age = 20;
	s.score = 90;
	s2.name = "老王";
	s2.age = 50;
	s2.score = 60;
	printStudent(&s);
	return 0;
}

8.8 结构体案例

8.8.1案例1:

#include<iostream>
#include<ctime>
using namespace std;
struct Student
{
	string sName;
	int score;
};
struct Teacher
{
	string tName;
	struct Student sArray[5];
};

void allocateSpace(struct Teacher tArray[], int len) //传递数组就是传递指针
{
	for (int i = 0; i < len ; i++)
	{
		string nameSeed = "ABCDE";
		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;//0~60+40
			tArray[i].sArray[j].score = random;
		}
	}
}

void printInfo(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));  //产生随机数种子
	/*unsigned int 为 "无符号整型",你可以理解为 unsigned int = -20和 int = 20
	输出结果是一样的为 "20”。无符号即理解为该数据类型为正整数.例如:
	int型的取值范围为-257-256 (不一定为此数值,依电脑处理位数不同而不同),
	则unsigned int的取值范围为 0-512。*/
	
	//创建三名老师的数组
	struct Teacher tArray[3];

	//通过函数给3名老师的信息赋值,并给老师带的学生信息赋值
	int len = sizeof(tArray) / sizeof(tArray[0]);
	allocateSpace(tArray,len);

	//打印所有老师及所带的学生信息
	printInfo(tArray,len);

	system("pause");
	return 0;
}

8.8.2案例2:

在这里插入图片描述在这里插入图片描述

#include<iostream>
using namespace std;

struct Hero
{
	string name;
	int age;
	string sex;

};

void bubbleSort(struct Hero hrr[], int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j + i < len - 1; j++)
		{
			if (hrr[j].age > hrr[j + 1].age)
			{
				struct Hero temp = hrr[j];
				hrr[j] = hrr[j + 1];
				hrr[j + 1] = temp;
			}
		}
	}
}
int main()
{
	Hero hrr[5] = 
	{ {"刘备",23,"男"},
	  {"关羽",22,"男"},
	  {"张飞",20,"男"},
	  {"赵云",21,"男"},
	  {"貂蝉",19,"女"} 
	};

	int len = sizeof(hrr) / sizeof(hrr[0]);
	for (int i = 0; i < len; i++)
	{
		cout << "姓名:" << hrr[i].name << " 年龄:" 
			<< hrr[i].age << " 性别:" << hrr[i].sex << endl;
	}

	bubbleSort(hrr, len);

	cout << "排完序之后的结果如下所示" << endl;
	for (int i = 0; i < len; i++)
	{
		cout << "姓名:" << hrr[i].name << " 年龄:"
			<< hrr[i].age << " 性别:" << hrr[i].sex << endl;
	}

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值