C++基础入门---8.结构体【P64~P71】
- 8. 结构体
- 8.1 结构体的定义和使用
- 8.2 结构体数组
- 8.3 结构体指针
- 8.4 结构体嵌套结构体
- 8.5 结构体作函数参数
- 8.6 结构体中const使用场景
- 8.7 结构体案例1
- 8.8 结构体案例2
8. 结构体
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
8.1 结构体的定义和使用
注:
1、定义结构体时的关键字是struct,不可以省略;
2、创建结构体变量时,关键字 struct 可以省略;
3、结构体变量利用操作符 “.” 访问成员。
#include<iostream>
#include<string>
using namespace std;struct student
{string name;int age;int score;
}s3;int main()
{//通过学生类型创建具体学生struct student s1;s1.age = 16;s1.name = "张三";s1.score = 60;cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;struct student s2 = { "李四",78,50 };cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;s3.name = "王五";s3.age = 20;s3.score = 60;cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;system("pause");return 0;
}
8.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,80},{"李四", 28, 90},{"王五", 38, 99}};//3、给结构体数组中的元素赋值stuArray[2].name = "赵六";stuArray[2].age = 19;stuArray[2].score = 85;//4、遍历结构体数组for (int i = 0; i < 3; i++){cout << "姓名:" <<stuArray[i].name << "年龄:" <<stuArray[i].age << "分数:" <<stuArray[i].score << endl;}system("pause");return 0;
}
8.3 结构体指针
作用:利用指针访问结构体中的成员。
- 利用操作符 -> 可以通过结构体指针访问结构体属性。
# include<iostream>
# include<string>using namespace std;//创建结构体变量
struct student
{string name;int age;int score;};int main()
{//创建学生结构体变量struct student s = { "张三",48,80 };//通过指针指向结构体变量struct student *p = &s;//通过指针访问结构体变量中的数据cout << "姓名:" << p->name << "年龄:" << p->age << "成绩:" << p->score << endl;system("pause");return 0;}
8.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;
};int main()
{//创建老师结构体变量teacher t;t.id = 10000;t.name = "老王";t.age = 50;t.stu.age = 18;t.stu.name="小李";t.stu.score = 90;cout << "老师姓名: " << t.name << "老师id: " << t.id<< "老师年龄:" << t.age << "学生年龄:" << t.stu.age<< "学生姓名:" << t.stu.name<< "学生成绩:" << t.stu.score << endl;system("pause");return 0;}
8.5 结构体作函数参数
# include<iostream>
# include<string>using namespace std;//定义结构体
struct student
{string name;int age;int score;
};
//1、值传递
void printstudent(struct student s)
{cout << "姓名:" << s.name << "年龄:" << s.age << "分数:" << s.score << endl;
}//2、地址传递
void printstudent2(struct student *p)
{cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
}int main()
{//创建老师结构体变量struct student s;s.name = "张三";s.age = 20;s.score = 60;printstudent(s);printstudent2(&s);system("pause");return 0;}
8.6 结构体中const使用场景
# include<iostream>
# include<string>using namespace std;//定义结构体
struct student
{string name;int age;int score;
};
//将函数中的形参改为指针,可以节省内存空间,而且不会赋值新的副本出来
void printstudent( const struct student *s)
{cout << "姓名:" << s->name << "年龄:" << s->age << "分数:" << s->score << endl;
}int main()
{//创建结构体变量struct student s;s.name = "张三";s.age = 20;s.score = 60;printstudent(&s);system("pause");return 0;
}
8.7 结构体案例1
# include<iostream>
# include<string>
# include<ctime>using namespace std;//定义结构体
struct student
{string name;int score;
};struct teacher
{string name;struct student sArray[5];};void allocateSpace(struct teacher tArray[] ,int len)
{string nameseed = "ABCDE";for (int i = 0; i < len; i++){tArray[i].name = "Teacher_";tArray[i].name += nameseed[i];for (int j = 0; j < 5; j++){tArray[i].sArray[j].name = "Student_";tArray[i].sArray[j].name += nameseed[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;cout << " 考试分数:" << tArray[i].sArray[j].score << endl;}}
}int main()
{//随机数种子srand((unsigned int)time(NULL));//1、创建3名老师的数组struct teacher tArray[3];//2、通过函数给3名老师的信息赋值,并给老师带的学生信息赋值int len = sizeof(tArray) / sizeof(tArray[0]);allocateSpace(tArray, len);//3、打印所有老师及所带学生的信息printInfo(tArray,len);system("pause");return 0;
}
8.8 结构体案例2
# include<iostream>
# include<string>using namespace std;//1、定义英雄结构体
struct hero
{string name;int age;string sex;
};void bubbleSort(struct hero array[], int len)
{for (int i = 0; i < len - 1; i++){for (int j = 0; j < len - i - 1; j++){if (array[j].age > array[j + 1].age){string temp = array[j + 1].name;array[j + 1].name = array[j].name;array[j].name = temp;}}}
}void printhero(struct hero array[], int len)
{for (int i = 0; i < len; i++){cout << "英雄姓名:" << array[i].name<< " 英雄年龄:" << array[i].age<< " 英雄性别:" << array[i].sex << endl;}
}int main()
{//2、创建英雄结构体数组存放5名英雄struct hero array[5]{{"刘备",23,"男"},{"关羽",22,"男"},{"张飞",20,"男"},{"赵云",21,"男"},{"貂蝉",19,"女"},};int len = sizeof(array) / sizeof(array[0]);/*for (int i = 0; i < len; i++){cout << "英雄姓名:" << array[i].name<< " 英雄年龄:" << array[i].age<< " 英雄性别:" << array[i].sex << endl;}*///3、对数组进行排序,按照年龄进行升序排序bubbleSort(array, len);//4、将排序结果打印输出printhero(array, len);system("pause");return 0;
}