1、结构体定义
#include<iostream>
#include<string>
using namespace std;
//1、创建学生数据类型: 学生包括(姓名,年龄,分数)
struct Student
{
string name;
int age;
int score;
}s3;//顺便创建结构体变量
//2、通过学生类型创建具体学生
int main()
{
//结构体创建时,关键字struct可以省略
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;
system("pause");
return 0;
}
2、结构体数组
#include<iostream>
#include<string>
using namespace std;
//结构体数组
//1、定义结构体
struct Student
{
string name;
int age;
int score;
};
int main()
{
//2、创建结构体数组
struct Student stuArray[3] =
{
{"张三",18,100},
{"李四",20,88,},
{"王五",30,66}
};
//3、遍历结构体数组
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << stuArray[i].name
<< "年龄:" << stuArray[i].age
<< "分数" << stuArray[i].score << endl;
}
system("pause");
return 0;
}
3、结构体指针
#include<iostream>
#include<string>
using namespace std;
//结构体数组
//1、定义结构体
struct Student
{
string name;
int age;
int score;
};
int main()
{
//1、创建结构体变量
struct Student s = { "张三",18,100 };
//2、通过指针指向结构体变量
struct Student *p = &s;
//3、通过指针访问结构体变量中的数据
cout << "姓名:" << p->name << "年龄" << p->age << "分数:" << p->score<<endl;
system("pause");
return 0;
}
4、结构体嵌套结构体
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name;
int age;
int score;
};
//定义老师结构体
struct teacher
{
int id;//教师编号
string name;//教师姓名
int age;//年龄
struct student stu;//辅导的学生
};
//打印学生信息函数
//1、值传递
void printStudent1(struct student s)
{
cout << "子函数中的姓名:" << s.name << "子函数中的年龄:"
<< s.age << "子函数中的分数:" << s.score << endl;
}
//2.地址传递
void printStudent2(struct student *p)
{
cout << "子函数2中的姓名:" << p->name << "年龄"
<< p->age << "分数:" << p->score << endl;
}
int main()
{
//结构体嵌套结构体
//创建老师
//将学生传入到一个参数中,打印学生身上的所有信息
teacher t;
t.id = 10000;
t.name = "老王";
t.age = 50;
t.stu.name = "小王";
t.stu.age = 18;
t.stu.score = 60;
cout << "老师姓名:" << t.name << "老师编号:"
<< t.id << "老师年龄:" << t.age
<< "老师辅导学生姓名" << t.stu.name
<< "学生年龄" << t.stu.age
<< "学生考试分数" << t.stu.score << endl;
struct student s =
{
"张三",
20,
85
};
printStudent1(s);
printStudent2(&s);
system("pause");
return 0;
}
5、结构体中const使用场景
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name;
int age;
int score;
};
//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
void printStudent(const student *s)
{
//s->age = 150;加入const之后,一旦有修改的操作就会报错,可以防止我们误操作
cout << "姓名:" << s->name << "年龄:" << s->age << "得分:"
<< s->score << endl;
}
int main()
{
//创建结构体变量
struct student s = { "张三",15,70 };
//通过函数打印结构体变量信息
printStudent(&s);
cout << "main中张三年龄为:" << s.age << endl;
system("pause");
return 0;
}
6、结构体案例
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
struct student
{
string sName;
int age;
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;
}
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
struct student
{
string sName;
int age;
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;
}