栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++学习记录(三)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++学习记录(三)

C++学习记录
    • 8.1 结构体
    • 8.2 结构体定义和使用
    • 8.3 结构体数组
    • 8.4 结构体指针
    • 8.5 结构体嵌套结构体
    • 结构体做函数参数
    • 8.7 结构体中const使用场景
    • 8.8 结构体案例
      • 8.8.1 案例一
      • 8.8.2 案例二

8.1 结构体

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

8.2 结构体定义和使用

语法 struct 结构体名 {结构体成员列表}
通过结构体创建变量的方式有三种:

  • struct 结构体名 变量名
  • struct 结构体名 变量名={成员1值,成员2值}
  • 定义结构体时顺便创建变量
//自定义的结构体,就是变量的集合
struct Student
{
	string name;
	int age;
}

//通过学生类型创建具体学生
struct Student s1;
s1.name = 'luna';
s1.ago = 21;

struct Student s2 = {'luna',21}; //创建时struct可以省略

struct Student
{
	string name;
	int age;
}s3;

总结1:定义结构体时关键字是struct,不可省略
总结2:创建结构体变量时,关键字struct可以省略
总结3:结构体变量的操作符 s1.name通过 " . "访问成员

8.3 结构体数组

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

#include
using namespace std;
#include
//结构体数组
//1. 定义结构体
struct Student
{
	string name;
	int age;
}
//2.创建结构体数组
int main(){}
	struct Student stuArray[3] =
	{
		{"zhangsan",18},
		{"lisi",20},
		{"wangwu",23}
	}
//3.给结构体数组中的元素赋值
stuArray[2].name = "zhaoliu" //将数组中第二个元素的name修改

//4.遍历结构体数组
for (int i=0;i<3;i++)
	{
	stuArray[i].name;
}
8.4 结构体指针

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

//结构体定义
struct student
{
	//成员列表
	string name;
	int age;
	
};

int main(){
//1.创建学生结构体变量
//2.通过指针指向结构体变量
//3.通过指针指向结构体变量中的数据
	struct student s = {"zhangsan", 18, 100};
	struct student *p = &s;
	p->name;
	p->age;
}

8.5 结构体嵌套结构体

作用:结构体中的成员可以是另一个结构体
例如:每个老师辅导一个学员,一个老师的结构体中还记录一个学生的结构体
示例:

//定义学生结构体
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 = 001;
	t.name = "teacher wang";
	t.age = 46;
	t.stu.name = "zhangsan";
	t.stu.age = 15;
	t.stu.score = 80;
}

结构体做函数参数

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

  • 值传递
  • 地址传递(形参变化会影响实参)
struct student
{
	string name;
	int age;
	int score;
};
//值传递
void printStudent1(struct student s){
	cout << s.name<
	cout<< p->name << p->age << p->score <
	student s;
	s.name = "zhangsan";
	s.age = 28;
	s.score = 80;
}

8.7 结构体中const使用场景

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

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

//const使用场景
void printStudent(const student *stu)	//加const防止误操作
{
	//stu->age = 20; 这里会赋值失败,只有有读的操作,有const不能写入值
	cout << stu->name << stu->age << stu->score < 
8.8 结构体案例 
8.8.1 案例一 

案例描述:
学生正在做毕设项目,每名老师带领5个学生,共有三名老师,需求如下:
设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员,学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值,最终打印出老师数据以及老师所带的学生数据。

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
	tArray[i].tName = "teacher_";
	tArray[i].tName += i;
	for (int j=0;j<5;j++)
	{
		int random = rand()%61 + 40  //40~100
		tArray[i].sArray[j].sName = "student_";
		tArray[i].sArray[j].sName += j;
		tArray[i].sArray[j].score = 60;
	}
	}
}
void printInfo(struct Teacher tArray[], int len)
{
	for(int i=0;i
		cout << tArray[i].tName <
			cout << tArray[i].sArray[j].score <
	//随机数种子 #include 
	srand(unsigned int)time(NULL);
	struct Teacher tArray[3];

	int len = sizeof(tArray) / sizeof(tArray[0]);
	allocateSpace(tArray,len);
}
8.8.2 案例二

案例描述:
设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。通过冒泡排序的算法,将数组中的英雄按照年龄进行生序排序,最终打印排序后的结果。
5名英雄信息:
{“刘备”,23,“男”},
{“关羽”,22,“男”},
{“张飞”,20,“男”},
{“赵云”,21,“男”},
{“貂蝉”,19,“女”}

#include
using namespace std;

struct Hero
{
	string name;
	int age;
	string sex;
};
//冒泡排序,实现年龄生序排序
void bubbleSort(struct Hero heroArray[], int len)
{
	for(int i=0;i
		for(int j=0;j
			if(heroArray[j].age>heroArray[j+1].age)
			{
				struct Hero tmp heroArray[j];
				heroArray[j] = heroArray[j];
				heroArray[j+1] = tmp;
				}
			}
		}
	}
int main(){
//创建数组存放5名英雄
	struct Hero heroArray[5]=
	{
		{"刘备",23,"男"},
		{"关羽",22,"男"},
		{"张飞",20,"男"},
		{"赵云",21,"男"},
		{"貂蝉",19,"女"},
	};
	int len = sizeof(heroArray)/sizeof(heroArray[0]);
	for(int i=0;i<)
	{
		cout << heroArray[i].name <
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1037847.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号