C++学习笔记 (12)—— 结构体

1、结构体基本概念

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

2、结构体的定义和使用

语法:

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

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

1.struct 结构体名 变量名 

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

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

 

eg:

#include <iostream>
#include <string>
using namespace std;
//1.创建学生数据类型:学生包括(姓名,年龄,分数)
//自定义数据类型,一些类型集合组成的一个类型
//语法 struct 类型名称 {成员列表}
//定义结构体变量时,struct 关键字可以省略(定义结构体时,不能省略)
struct Student
{
//成员列表

//姓名
string name;
//年龄
int age;
//分数
int score;
}s3;
//2.通过学生类型创建具体学生
//三种方式
//2.1 struct Student s1
//2.2 struct Student s2 ={ 。。。}
//2.3 在定义结构体时顺便创建结构体变量
int main()
{
cout << "结构体练习:" << endl;
//1 struct Student s1
//结构体变量 通过 . 即成员访问运算符,访问成员变量
struct Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 95;
cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 成绩:" << s1.score << endl;
//2 struct Student s2 ={ 。。。}
//定义结构体变量时,struct 关键字可以省略(定义结构体时,不能省略)
Student s2 = { "李四",25,60 };
cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 成绩:" << s2.score << endl;
//3 在定义结构体时顺便创建结构体变量
s3 = { "王五",20,85 };
cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 成绩:" << s3.score << endl;
system("pause");
return 0;
}

 

3.结构体和数组

作用:

将自定义的结构体放到数组中方便维护

语法:

struct 结构体名 [元素个数] = { {},{},。。};

eg:

#include <iostream>
#include <string>
using namespace std;
//结构体数组
//1.定义结构体
struct Student
{
//姓名
string name;
//年龄
int age;
//分数
int score;
};

int main()
{
cout << "结构体数组练习:" << endl;
//2.创建结构体数组
Student StuArray[3] =
{
{"张三",18,100},
{"李四",81,65},
{"王五",17,99}
};
//3.给结构体数组中的元素赋值
//比如修改一下姓名
StuArray[0].name = "小屈";
//4.遍历结构体数组
for (int i = 0; i < 3; i++)
{
cout << StuArray[i].name <<" 成绩 : " ;
cout << StuArray[i].age <<" 年龄 :";
cout << StuArray[i].score << endl;
}
system("pause");
return 0;
}

 

4.结构体指针

作用

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

  • 利用成员访问运算符  - >可以通过结构体指针访问结构体属性

eg:

#include <iostream>
#include <string>
using namespace std;
//结构体指针

//定义学生结构体

struct Student
{
//姓名
string name;
//年龄
int age;
//成绩
int score;
};
int main()
{
cout << "结构体指针练习:" << endl;
//1.创建学生结构体变量
Student student;
student.name = "小杜";
student.age = 18;
student.score = 90;
//2.通过指针指向结构体变量
Student* pstudent = &student;
//3.通过指针访问结构体变量中的数据
cout << "姓名:"<<pstudent->name << " 年龄:"<<pstudent->age <<" 成绩:"<< pstudent->score << endl;
system("pause");
return 0;
}

5.结构体中嵌套结构体

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

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

eg:

#include <iostream>
#include <string>

using namespace std;
//例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体

//定义学生结构体

struct Student
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
};
//定义老师结构体
struct Teacher
{
//成员列表
//职工编号
int id;
//教师姓名
string name;
//教师年龄
int age;
//子结构体 学生
Student student;

};


int main()
{
cout << "结构体嵌套结构体练习:" << endl << endl;
//1.定义老师1结构体变量
Teacher teacher1;
//2.补充老师1信息
teacher1.name = "史老师";
teacher1.age = 56;
teacher1.id = 11;
//3.定义老师1下的学生信息
teacher1.student.name = "李白";
teacher1.student.age = 16;
teacher1.student.score = 89;

//1.定义老师2结构体变量
Teacher teacher2;
//2.补充老师2信息
teacher2.name = "芈老师";
teacher2.age = 45;
teacher2.id = 12;
//3.定义老师2下的学生信息
teacher2.student.name = "小凯";
teacher2.student.age = 21;
teacher2.student.score = 100;

cout <<"老师姓名:\t\t" << teacher1.name << endl
<< "老师id: \t\t" << teacher1.id << endl
<< "老师年龄:\t\t" << teacher1.age << endl
<< "老师所带的学生姓名:\t" << teacher1.student.name << endl
<< "老师所带的学生年龄:\t" << teacher1.student.age << endl
<< "老师所带的学生成绩:\t" << teacher1.student.score << endl;

cout << "-----------------------------------" << endl;
cout << "老师姓名:\t\t" << teacher2.name << endl
<< "老师id: \t\t" << teacher2.id << endl
<< "老师年龄:\t\t" << teacher2.age << endl
<< "老师所带的学生姓名:\t" << teacher2.student.name << endl
<< "老师所带的学生年龄:\t" << teacher2.student.age << endl
<< "老师所带的学生成绩:\t" << teacher2.student.score << endl;
system("pause");
return 0;
}

 

6.结构体做函数参数

作用:

  • 将结构体作为参数向函数中传递

传递方式有两种:

  • 值传递
  • 地址传递

eg:

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

//1.定义学生结构体
struct Student
{
//姓名
string name;
//年龄
int age;
//分数
int score;
};
//2.声明函数 值传递
void stuPrint(Student student);

//声明函数 地址传递
void pstuPrint(Student *student);
int main()
{
cout << "结构体做函数参数:" << endl << endl;
//3.定义学生1结构体变量
Student student1;
student1.name = "齐宣王";
student1.age = 19;
student1.score = 98;

//4.定义学生2结构体变量
Student student2;
student2.name = "楚怀王";
student2.age = 32;
student2.score = 100;
stuPrint(student1);
stuPrint(student2);
cout << endl << endl;
pstuPrint(&student1);
pstuPrint(&student2);


system("pause");
return 0;
}

void stuPrint(Student student)
{
cout << "值传递:" << endl << endl;
cout << "学生姓名:"<<student.name
<< " 年龄:"<<student.age
<<" 分数:"<< student.score
<< endl;
cout << "--------------------" << endl;

}
void pstuPrint(Student *student)
{
cout << "地址传递:" << endl << endl;
cout << "学生姓名:" << student->name
<< " 年龄:" << student->age
<< " 分数:" << student->score
<< endl;
cout << "**********************" << endl;
}

 

8、结构体案例

8.1 案例

案例描述:

学校在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下:

<1.设计学生和老师的结构体.
<2.其中在老师的结构体中: 有老师的姓名和一个存放5名学生的数组作为成员
<3.学生的成员有:姓名、考试分数.
<4.创建数组存放3名老师.
<5.通过函数给每个老师及所带的学生赋值
<6.最终打印出老师数据以及老师所带的学生数据

 eg:

#include <iostream>
#include <string>
using namespace std;
// 案例描述:
//
// 学校在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下:
//
// 1.设计学生和老师的结构体.
// 2.其中在老师的结构体中: 有老师的姓名和一个存放5名学生的数组作为成员
// 3.学生的成员有:姓名、考试分数.
// 4.创建数组存放3名老师.
// 5.通过函数给每个老师及所带的学生赋值
// 6.最终打印出老师数据以及老师所带的学生数据

//创建学生结构体
struct Student
{
//学生姓名
string sName;
//分数
int score;
//
};
//创建老师结构体
struct Techar
{
//老师姓名
string tName;
//5个学生
Student Stuarray[5];
};

//声明函数 赋值操作
void asment(Techar* teacher);
//声明函数 打印数据操作
void printData(Techar* techar,int Tlen);
int main()
{
cout << "结构案例1练习:" << endl << endl;
//创建数组存放3名老师
Techar teacher[3];
//通过数组给3名老师及所带学生赋值
asment(teacher);
//打印所有数据
int Tlen = sizeof(teacher);
//cout << "Tlen= " << Tlen << endl;
int len = sizeof(teacher[0]);
//cout << "len= " << len << endl;
int Ttlen = Tlen / len;
//cout << "Ttlen= " << Ttlen << endl;
//cout << "----------" << endl;
printData(teacher,Ttlen);
system("pause");
return 0;
}

 

 

void asment(Techar* teacher)
{
teacher[0].tName = "秦老师";
teacher[0].Stuarray[0].sName = "甄姬";
teacher[0].Stuarray[0].score = 100;
teacher[0].Stuarray[1].sName = "貂蝉";
teacher[0].Stuarray[1].score = 100;
teacher[0].Stuarray[2].sName = "马克";
teacher[0].Stuarray[2].score = 68;
teacher[0].Stuarray[3].sName = "项羽";
teacher[0].Stuarray[3].score = 99;
teacher[0].Stuarray[4].sName = "荆轲";
teacher[0].Stuarray[4].score = 85;

teacher[1].tName = "池老师";
teacher[1].Stuarray[0].sName = "诸葛亮";
teacher[1].Stuarray[0].score = 77;
teacher[1].Stuarray[1].sName = "虞姬";
teacher[1].Stuarray[1].score = 100;
teacher[1].Stuarray[2].sName = "典韦";
teacher[1].Stuarray[2].score = 68;
teacher[1].Stuarray[3].sName = "剑姬";
teacher[1].Stuarray[3].score = 60;
teacher[1].Stuarray[4].sName = "孙尚香";
teacher[1].Stuarray[4].score = 85;


teacher[2].tName = "墨老师";
teacher[2].Stuarray[0].sName = "张良";
teacher[2].Stuarray[0].score = 98;
teacher[2].Stuarray[1].sName = "露娜";
teacher[2].Stuarray[1].score = 100;
teacher[2].Stuarray[2].sName = "廉颇";
teacher[2].Stuarray[2].score = 100;
teacher[2].Stuarray[3].sName = "花木兰";
teacher[2].Stuarray[3].score = 60;
teacher[2].Stuarray[4].sName = "鲁班";
teacher[2].Stuarray[4].score = 85;

}
void printData(Techar* teacher,int Tlen)
{
//cout << "进入打印函数" << endl;
int Slen = sizeof(teacher[0].Stuarray) / sizeof(teacher[0].Stuarray[0]);
//cout << "Slen= " << Slen << endl;
for (int i = 0; i < Tlen; i++)
{
cout << "老师姓名:" << teacher[i].tName << endl << endl;
cout << "老师所带学生:" << endl << endl;
for (int j = 0; j < Slen; j++)
{
cout << " 学生"<<j+1<< endl
<<"\t姓名:" << teacher[i].Stuarray[j].sName << endl
<< "\t成绩:" << teacher[i].Stuarray[j].score << endl;
}
cout <<"------------------------------"<< endl;
}

}

 8.2案例

案例描述:

设计一个人物的结构体,包括成员姓名、年龄、性别;

创建结构体数组,数组中存放5名英雄

通过冒泡排序的算法,将数组中的人物按照年龄进行升序排序,最终打印排序后的结果。

 

5名英雄信息如下:

{"刘备",23,"男"}

{"虞姬",69,"女"}

{"孙权",45,"男"}

{"花木兰",24,"女"}

{"村夫",32,"男"}

posted @ 2022-04-11 22:50  雾枫  阅读(83)  评论(0编辑  收藏  举报