1、结构体的定义和使用
cpp#include <iostream> using namespace std; #include <string> struct Student { string name; int age; int score; }s3; int main() { //1、 struct Student s1; s1.name = "张三"; s1.age = 18; s1.score = 100; cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl; //2、 struct Student s2 = { "李四",19,80 }; cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl; //3、 s3.name = "王五"; s3.age = 20; s3.score = 60; cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl; system("pause"); return 0; }
结构体变量创建的时候 struct可以省略
2、结构体数组
cpp#include <iostream> using namespace std; #include <string> struct Student { string name; int age; int score; }s3; int main() { struct Student stuArray[3] = { {"张三",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; }
3、结构体指针
cpp#include <iostream> using namespace std; #include <string> struct Student { string name; int age; int score; }; int main() { struct Student s = { "张三",18,100 }; struct Student* p = &s; cout << "姓名:" << p->name << " 年龄:" << p->age << " 成绩:" << p->score << endl; system("pause"); return 0; }
4、结构体嵌套结构体
cpp#include <iostream> using namespace std; #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 = 60; cout << "老师姓名:" << t.name << " 老师编号:" << t.id << " 老师年龄:" << t.age << " 老师辅导的学生姓名:" << t.stu.name << " 学生年龄:" << t.stu.age << " 学生成绩:" << t.stu.score << endl; system("pause"); return 0; }
5、结构体做函数参数
cpp#include <iostream> using namespace std; #include <string> struct student { string name; int age; int score; }; void printfStudent1(struct student s) { cout << "在子函数中打印 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl; } void printfStudent2(struct student* p) { cout << "子函数2中 姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl; } int main() { struct student s; s.name = "张三"; s.age = 20; s.score = 85; //cout << "main函数中打印 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl; //printfStudent1(s); printfStudent2(&s); system("pause"); return 0; }
6、结构体中const的使用
cpp#include <iostream> using namespace std; #include <string> struct student { string name; int age; int score; }; void printfStudent(const student *s) { //s->age = 150;加入const之后,一旦有修改的操作就会报错,可以防止误操作 cout << "姓名:" << s->name << " 年龄:" << s->age << " 分数:" << s->score << endl; } int main() { struct student s; s.name = "张三"; s.age = 15; s.score = 70; printfStudent(&s); system("pause"); return 0; }
7、案例一
案例描述:
学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员。
学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值,最终打印出老师数据以及老师所带的学生数据。
cpp#include <iostream> using namespace std; #include <string> #include <ctime> struct student { string sName; int score; }; struct Teacher { string tName; struct student sArray[5]; }; void allocatSpace(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;//40`100 tArray[i].sArray[j].score = random; } } } void printfInfo(struct Teacher tArray[], int len) { for (int i = 0; i < len; i++) { cout << "老师的姓名:" << tArray[i].tName << endl; for (int j = 0; j < len; j++) { cout << "\t学生的姓名:" << tArray[i].sArray[j].sName << " 考试分数:" << tArray[i].sArray[j].score << endl; } } } int main() { srand((unsigned int)time(NULL)); Teacher tArray[3]; int len = sizeof(tArray) / sizeof(tArray[0]); allocatSpace(tArray, len); printfInfo(tArray,len); system("pause"); return 0; }
8、案例二
案例描述:
设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。
通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
cpp#include <iostream> using namespace std; #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 - i - 1; j++) { if (heroArray[j].age > heroArray[j + 1].age) { struct Hero temp = heroArray[j]; heroArray[j] = heroArray[j + 1]; heroArray[j + 1] = temp; } } } } void printfHero(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]); bubbleSort(heroArray, len); printfHero(heroArray, len); system("pause"); return 0; }
结构体(个人学习笔记黑马学习)
寻心的无心者2023-08-31 21:51