跳到主要内容

8,结构体

struct 结构体名 变量名

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

定义结构体顺便创建变量

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

/*
struct Student
{
string name;
int age;
int score;
}s3;
*/

//struct Student s1;
//struct Student s2 = {..};
//在定义结构体时顺便创建结构体变量
int main()
{
struct Student s1;//struct可以省略
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;


system("pause");
return 0;
}

8.3结构体数组

#include<string>

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

int main()
{
struct Student stuArray[8] =
{
{"张三",18,100},
{"李四",28,99},
{"王五",38,66}
};

stuArray[2].name = "赵六";
stuArray[2].age = 80;
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;
}

8.4结构体指针

#include<string>

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

int mian()
{
struct student s ={"张三",18,100};

struct student *p = &s;

cout<<"姓名:"<<p->name
<<"年龄:"<<p->age
<<"分数:"<<p->score<<endl;

system("pause");
return 0;
}

8.5结构体嵌套结构体

#include<string>

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 = 20;
t.stu.score = 100;
cout<<"老师的姓名:"<<t.name<<"老师的年龄:"<<t.age<<"老师的编号:"<<t.id<<"学生的姓名:"<<t.stu.name <<"学生的年龄:"
<<t.stu.age<<"学生的分数:"<<t.stu.score<<endl;
system("pause");
return 0;
}

8.6结构体做函数的参数

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

void printStudent1(struct student s)
{
cout<<"子函数1中,姓名:"<<s.name<<"年龄:"<<s.age<<"分数:"<<s.score;//值传递
}

void printStudent2(struct student *p)
{
p->age = 100;//主函数也会改变
cout<<"子函数2中,姓名:"<<s->name<<"年龄:"<<s->age<<"分数:"<<s->score;//地址传递
}

int main()
{
struct student t;
s.name = "张三";
s.age = 20;
s.score = 85;

cout<<"主函数中,姓名:"<<s.name<<"年龄:"<<s.age<<"分数:"<<s.score;
printStudent1(s);
printStudent2(&s);

system("pause");
return 0;
}

8.7结构体中const使用场景

#include<string>

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

//将函数中的形参改为指针,可以减少内存空间,而不会复制新的副本
void printStudent(const struct student *s)//一旦修改就会报错
{
cout<<"姓名:"<<s->name<<"年龄:"<<s->age<<"分数:"<<s->score;//地址传递
}

int mian()
{
struct student s= {"张三",15,70};

printStudent(&s);

system("pause");
return 0;
}

8.8.1案例1

#include<string>
#include<ctime>

struct student
{
string name;
int score;
};

struct Teacher
{
string tName;
struct student sArray[5];
};

void allocateSpace(struct 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 = 60;
}
}
}

printInfo(struct 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));
struct Teacher tArray[3];

int len =sizeof(tArray)/sizeof(tArray[0]);
allocateSpace(tArray,len);

printInfo(tArray,len);

system("pause");
return 0;
}

8.8.2案例2

#include<string>

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

void bubbleSort(struct Hero heroArray[],int len)
{
for(int i = 0; i<len-1;i++)
{
for(int j = 0;j<len-1-i;j++)
{
if(ArrayHero[j].age > heroArray[j+1].age)
{
struct Hero temp = heroArray[j];
heroArray[j] = heroArray[j+1];
heroArray[j+1] = temp;
}
}
}
}

void printHero(struct Hero heroArray[],int len)
{
for(int i = 0; i<len; i++)
{
cout<<"排序后:"<<"姓名:"<<heroArray[i].name<<"年龄:"<<heroArray[i].age<<"姓别:"<<heroArray[i].sex<<endl;
}
}

int main()
{
struct Hero heroArray[5] =
{
{"刘备"23"男"}
{"关羽"22"男"}
{"张飞"20"男"}
{"赵云"21"男"}
{"貂蝉"19"女"}
};

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

bubbleSort(heroArray,len);
printHero(heroArray,len);
system("pause");
return 0;
}

参考与致谢