一、 结构体
属于用户自定义的数据类,允许用户存储不同的数据类型
语法:
struct 结构体名{ 结构体成员列表 };
通过结构体创建变量的方式:
1、struct 结构体名 变量名
2、struct 结构体名 变量名 = { 成员1值, 成员2值…}
3、定义结构体时,顺便创建变量
//结构体定义
struct stduent{
string name;
int age;
int score;
}s3;//第三种:顺便创建结构体变量
int main(){
//第一种创建结构体变量方式
struct Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << "姓名:"<< s1.name << "年龄:" << s1.age << "分数:" << s1.score << endl;
//第二种创建结构体变量方式
struct Student s2 = {"李四", 19, 80};
cout << "姓名:"<< s2.name << "年龄:" << s2.age << "分数:" << s2.score << endl;
//第三种创建结构体变量方式,定义结构体时,在末尾顺便创建了变量
s3.name = "王五";
s3.age = 20;
s3.score = 60;
cout << "姓名:"<< s3.name << "年龄:" << s3.age << "分数:" << s3.score << endl;
return 0;
}
注意:
1、定义结构体时,关键字struct,不可省略
2、创建结构体变量时,关键字struct,可以省略
3、结构体变量利用操作符 “.” 访问成员
二、 结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法:
struct 结构体名 数组名[ 元素个数 ] = { { }, { }, … ,{ } }
//1、定义结构体
struct Student{
string name;
int age;
int score;
};
int main(){
//2、创建结构体数组
struct Student stuArray[3]={
{"张三", 18, 100},
{"李四", 28, 99},
{"王五", 38, 66}
};
//3、结构体数组中的元素赋值(修改值)
stuArray[2].name = "赵六";
stuArray[2].age = 80;
stuArray[2].score = 60;
//4、遍历结构体数组
for (int i = 0; i<3; i++){
cout << "姓名:"<< stuArray[i].name << "年龄:" << stuArray[i].age << "分数:" << stuArray[i].score << endl;
}
}
三、 结构体指针
作用:通过指针访问结构体中的成员
操作符 : ->
struct Student{
string name;
int age;
int score;
};
int main(){
//1、创建结构体变量
struct Student s = {"张三", 18, 95};
//2、通过指针指向结构体变量
struct Student *p = & s;
//3、通过指针访问结构体变量中的数据
cout << "姓名:"<< p -> name << "年龄:" << p -> age << "分数:" << p -> score << endl;
return 0;
}
四、 结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
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 = 50;
t.stu.name = "小王";
t.stu.age = 18;
t.stu.score = 80;
return 0
}
五、结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式:
1、值传递
2、地址传递
struct student{
string name;
int age;
int score;
};
//值传递
void printStudent1(sturct student s){
cout << "姓名:"<< s.name << "年龄:" << s.age << "分数:" << s.score << endl;
}
//地址传递
void printStudent1(sturct student *p){
cout << "姓名:"<< p -> name << "年龄:" << p -> age << "分数:" << p -> score << endl;
}
int main(){
sturct student s = {"张三", 20, 85};
//通过打印函数,输出学生信息
//1\值传递
printStudent1(s);
//2\地址传递
printStudent2(&s);
return 0;
}
注意:不想修改主函数中的数据,用值传递,反之用地址传递
六、结构体中的const
作用:用const防止误操作
struct student{
string name;
int age;
int score;
};
//将函数中的形参改成指针,可以减少内存空间,不会复制新的副本
void printStudent(const sturct student *s){
//s->age = 100; //加了const就会报错,因此防止误操作
cout << "姓名:"<< s->name << "年龄:" << s->age << "分数:" << s->score << endl;
}
int main(){
sturct student &s = {"张三", 20, 85};
//通过函数打印结构体变量信息
printStudent(&s);
return 0;
}
结构体案例,综合以上
#include<iostream>
#include<string>
using namespace std;
/*
每个老师带领5个学生,总共3个老师,设计老师和学生的结构体
学生有姓名,考试分数
创建数组存放3个老师,通过函数给每个老师及所带学生复制
最终打印老师以及学生数据
*/
struct student {
string name;
int score;
};
struct teacher {
string name;
struct student sArray[5];
};
void allocateSpace(struct teacher tArray[], int len) {
string neameseed = "ABCDE";
//给老师赋值
for (int i = 0; i < len; i++) {
tArray[i].name = "Teacher_";
tArray[i].name = tArray[i].name + neameseed[i];
//通过循环给所带学生赋值
for (int j = 0; j < 5; j++) {
tArray[i].sArray[j].name = "Student_";
tArray[i].sArray[j].name += neameseed[j];
//随机给分数
int random = rand() % 61 + 40; // 40~100之间分数
tArray[i].sArray[j].score = random;
}
}
}
void printInfo(struct teacher tArray[], int len) {
for (int i = 0; i < len; i++) {
cout << "老师名字:" << tArray[i].name << endl;
for (int j = 0; j < 5; j++) {
cout << "\t学生姓名:" << tArray[i].sArray[j].name <<
"考试分数:" << tArray[i].sArray[j].score << endl;
}
}
}
int main() {
//创建老师结构体数组
struct teacher tArray[3];
//通过函数给3名老师信息赋值,并给老师带的学生信息赋值
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, len);
//打印所有信息
printInfo(tArray, len);
return 0;
}