目录
结构体
概念:用户自定义的数据类型,允许用户存储不同的数据类型。
#include<iostream>
using namespace std;
#include<string>
//创建定义时不可以省略
struct Student { //自定义数据类型,一些类型集合组成的一个类型
string name;
int age;
int score;
}s3;//顺便创建一个结构体变量
int main()
{
//Student s1 //struct关键字创建变量的时候可以省略
struct Student s1; //给s1赋属性,通过.访问结构体变量中的属性
s1.name = "tony";
s1.age = 14;
s1.score = 90;
cout << "name:" << s1.name << " age:" << s1.age << " score:" << s1.score <<endl;
struct Student s2 = { "tom",15,99 };//直接给s2赋属性
cout << "name:" << s2.name << " age:" << s2.age << " score:" << s2.score << endl;
s3.name = "petter"; //直接赋值,一般前两种比较多,第三种了解即可
s3.age = 18;
s3.score = 70;
cout << "name:" << s3.name << " age:" << s3.age << " score:" << s3.score << endl;
system("pause");
return 0;
}
结构体数组
struct Student stuArray[3] = {
{"tony",18,100},
{"tom",13,99},
{"petter",15,77}
};
stuArray[2].name = "katty";
stuArray[2].age = 23;
stuArray[2].score = 88;
int len = sizeof(stuArray) / sizeof(stuArray[0]);
for (int i = 0;i < len;i++) {
cout << "name:" << stuArray[i].name
<< " age:" << stuArray[i].age
<< " score:" << stuArray[i].score << endl;
}
结构体指针
struct student s = {//s就类似指针
"tony",18,100
};
struct student * p = &s;//指针通过->来引用结构体中的内容
cout << "name = " << p->name << " age:" << p->age << " score:" << p->score << endl;
结构体嵌套结构体
在结构体定义另外一个结构体作为成员,用来解决实际问题
struct student {
string name;
int age;
int score;
};
struct teacher {
int id;
string name;
int age;
struct student stu;
};
结构体做函数参数
#include<iostream>
using namespace std;
#include<string>
struct student {
string name;
int age;
int score;
};
void printStudent1(struct student s) {
s.score = 100;
cout << "值传递\t\tname:" << s.name << " age:" << s.age << " score:" << s.score << endl;
}
void printStudent2(struct student* s) {
s->score = 100;
cout << "地址传递\tname:" << s->name << " age:" << s->age << " score:" << s->score << endl;
}
int main()
{
struct student s = {
"tony",20,80
};
cout << "主函数\t\tname:" << s.name << " age:" << s.age << " score:" << s.score << endl;
printStudent1(s);
cout << "值传递后\tname:" << s.name << " age:" << s.age << " score:" << s.score << endl;
printStudent2(&s);
cout << "地址传递后\tname:" << s.name << " age:" << s.age << " score:" << s.score << endl;
system("pause");
return 0;
}
const
防止误操作:
将函数中的指针改为内存,可以减少内存空间占用,而且不会复制一个新的副本。
void printStudents(const student *s) {
//s -> age = 20;//加入const之后,一旦有修改就会报错,可以防止误操作
cout << "name:" << s->name << " age:" << s->age << " score:" << s->score << endl;
}
案例1:字符命名运用以及随机数运用、函数结构体数组运用
学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下:
设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员,学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值。
注:函数形参中数组名称后加[]可以明显看出其数据类型,同时需要取消*号。
#include<iostream>
using namespace std;
#include<string>
struct student {
string name;
int score;
};
struct teacher {
string name;
struct student stu[3][5];
};
void inputStudent(struct student* arrStu,int num,int length);
void inputTeacher(struct teacher* arrTea,int length);
int main()
{
struct teacher arrTeacher[3];
int len = sizeof(arrTeacher) / sizeof(arrTeacher[0]);
inputTeacher(arrTeacher,len);
system("pause");
return 0;
}
void inputTeacher(struct teacher* arrTea,int length) {
for (int i = 0;i < length;i++) {
cout << "请输入教师姓名: ";
cin >> arrTea[i].name;
int len = sizeof(arrTea->stu[0]) / sizeof(arrTea->stu[0][0]);
inputStudent(arrTea->stu[i],i,len);
}
}
void inputStudent(struct student* arrStu,int num,int length) {
for (int i = 0;i < length;i++) {
cout << "请输入学生姓名:";
cin >> arrStu[i].name;
cout << "请输入学生分数:";
cin >> arrStu[i].score;
}
}
注:1.随机数
#include<ctime>//需要提前声明随机性
int main(){
srand((unsigned int)time(NULL));
//需要提前声明来保证随机性
int random = rand() % 61 + 40;
//int代表整数,%后代表0~60的随机整数,后面可以加任意整数
}
2.字符命名
#include<string>
int main(){
string nameSeed = "ABCED";
string tArray[5];
for (int i = 0;i < 5;i++){
tArray[i] = "Teacher_";
tArray[i] += nameSeed[i];
}
}
案例2:结构体数组冒泡排序
设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。
通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
#include<iostream>
using namespace std;
#include<string>
struct temp {
string name;
int age;
string sex;
};
struct hero {//英雄结构体
string name;
int age;
string sex;
};
void ageSort(struct hero heroArray[], int len) {//排序
for (int i = 0;i < len - 1;i++) {
struct temp t;
for (int j = 0;j < len - i - 1;j++) {
if (heroArray[j].age > heroArray[j + 1].age) {
t.name = heroArray[j].name;
t.age = heroArray[j].age;
t.sex = heroArray[j].sex;
heroArray[j].name = heroArray[j + 1].name;
heroArray[j].age = heroArray[j + 1].age;
heroArray[j].sex = heroArray[j + 1].sex;
heroArray[j + 1].name = t.name;
heroArray[j + 1].age = t.age;
heroArray[j + 1].sex = t.sex;
}
}
}
}
void printHero(struct hero heroArray[], int len) {//排序后结果打印输出
for (int i = 0;i < len;i++) {
cout << heroArray[i].name << "\t" << heroArray[i].age << "\t" << heroArray[i].sex << endl;
}
}
int main() {
struct hero hArray[] = {//创建数组存放5名英雄
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"},
};
int len = sizeof(hArray) / sizeof(hArray[0]);
ageSort(hArray, len);
printHero(hArray, len);
system("pause");
return 0;
}
注:中间量直接通过结构体数据类型接受相同结构体内容。
struct Hero temp = heroArray[j];
heroArray[j] = heroArray[j + 1];
heroArray[j + 1] = temp;