结构体
结构体定义和使用
语法:struct 结构体名 { 结构体成员列表 };
通过结构体创建变量的方式有三种:.
- struct 结构体名变量名
- struct结构体名变量名={成员1值,成员2…}
- 定义结构体时顺便创建变量
//创建学生数据类型:学生包括(姓名,年龄,分数)
struct Student
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
};
int main()
{
//结构体变量创建方式1
struct Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;
//2.结构体变量创建方式2
struct Student s2 = { "李四",19,80 };
cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;
}
结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名[元素个数] ={ {},{},...{} }
#include<iostream>
#include<string>
using namespace std;
//结构体定义
struct Student
{
//成员列表
string name;//姓名
int age;//年龄
int score;//分数
};
int main()
{
//创建结构体数组
struct Student stuArray[3]=
{
{"张三",18,100},
{"李四",19,80 },
{"王五",20,90 }
};
//给结构体数组中的元素赋值
stuArray[2].name = "赵六";
stuArray[2].age = 30;
stuArray[2].score = 60;
//遍历结构体数组
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << stuArray[i].name << " 年龄:" << stuArray[i].age
<< " 分数:" << stuArray[i].score << endl;
}
system("pause");
return(0);
}
输出:
姓名:张三 年龄:18 分数:100
姓名:李四 年龄:19 分数:80
姓名:赵六 年龄:30 分数:60
结构体指针
作用:通过指针访问结构体中的成员
利用操作符->
可以通过结构体指针访问结构体属性
int main()
{
//创建结构体变量
struct student s = { "张三",18,100 };
//通过指针指向结构体变量
student* p = &s;
//通过指针访问结构体变量中的数据
cout << "姓名:" << p->name << " 年龄:" << p->age
<< " 分数:" << p->score << endl;
}
结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
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 = "老王";
t.age = 40;
t.stu.name = "小王";
t.stu.age = 20;
t.stu.score = 100;
cout << " 老师姓名:" << t.name << " 老师编号:" << t.id<<" 老师年龄:" << t.age<< " 学生名字:" << t.stu.name << " 学生年龄:" << t.stu.age<<" 学生分数:" << t.stu.score << endl;
}
结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式有两种:值传递和地址传递
#include<iostream>
#include<string>
using namespace std;
//结构体定义
struct student
{
string name;
int age;
int score;
};
//1.值传递
void printStudents1(struct student s)
{
cout << "姓名:" << s.name << " 年龄:" << s.age << " 得分:" << s.score << endl;
}
//2.地址传递
void printStudents2(struct student* p)
{
cout << "姓名:" << p->name << " 年龄:" << p->age << " 得分:" << p->score << endl;
}
int main()
{
//创建结构体变量
struct student s = { "张三",15,70 };
//通过函数打印结构体变量信息
printStudents2(&s);
system("pause");
return(0);
}
结构体中const使用场景
作用:用const来防止误操作
#include<iostream>
#include<string>
using namespace std;
//结构体定义
struct student
{
string name;
int age;
int score;
};
void printStudents(student s)
{
cout << "姓名:" << s.name << " 年龄:" << s.age << " 得分:" << s.score << endl;
}
int main()
{
//创建结构体变量
struct student s = { "张三",15,70 };
//通过函数打印结构体变量信息
printStudents(s);
system("pause");
return(0);
}
#include<iostream>
#include<string>
using namespace std;
//结构体定义
struct student
{
string name;
int age;
int score;
};
//void printStudents(student s)
//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的数据出来
void printStudents(student *s)
{
//s->age=150;//加入const之后,一旦有修改的操作就会报错防止误操作
cout << "姓名:" << s->name << " 年龄:" << s->age << " 得分:" << s->score << endl;
}
int main()
{
//创建结构体变量
struct student s = { "张三",15,70 };
//通过函数打印结构体变量信息
printStudents(s);
system("pause");
return(0);
}
结构体案例
案例描述:学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值最终打印出老师数据以及老师所带的学生数据。
#include<iostream>
#include<string>
#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)
{
string nameSeed = "ABCDE";
//给老师开始赋值
for (int i = 0; i < len; i++)
{
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;
tArray[i].sArray[j].score = random;
}
}
}
//打印所有信息
void printInfo(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].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);
//打印所有老师及所带的学生信息
printInfo(tArray,len);
system("pause");
return(0);
}