【2021-02-07】结构体的相关操作

 1 #include<iostream>
 2 using namespace std;
 3 #include<string>
 4 //1.创建学生的数据类型 :学生包括(姓名年龄分数) 
 5 //自定义数据类型,一些类型的集合组成的一个类型
 6 //语法 struct 类型名称{成员列表} 
 7 struct Student
 8 {
 9     //成员列表
10     
11     //姓名
12     string name;
13     //年龄
14     int age;
15     //分数 
16     int score;
17 }s3; //顺便创建一个结构体变量 
18 
19 //2.通过 学生类型创建具体学生
20 
21 //2.1 struct Student s1  
22 //2.2 struct Student s2 = {...} 
23 //2.3 在定义结构体时顺便创建结构体变量 
24 int main(){
25     
26     struct Student s1; //结构体创建的时候关键字可以省略
27     //Student s1; 也是正确的 
28     
29     //给s1属性赋值,通过.访问结构体变量中的属性
30     s1.name="张三";
31     s1.age = 18; 
32     s1.score = 100;
33     
34     cout <<"姓名:"<< s1.name <<"  年龄:" << s1.age << "  分数:" << s1.score <<endl; 
35     
36     
37     struct Student s2 = {"李四",19,80};
38     cout <<"姓名:"<< s2.name <<"  年龄:" << s2.age << "  分数:" << s2.score <<endl; 
39     
40     
41     s3.name="王五";
42     s3.age=20;
43     s3.score = 60;
44     cout <<"姓名:"<< s3.name <<"  年龄:" << s3.age << "  分数:" << s3.score <<endl;  
45     
46     system("pause");
47     
48     return 0;
49 } 
结构体的基本概念
 1 #include<iostream>
 2 using namespace std;
 3 #include<string>
 4 
 5 //1.定义结构体
 6 
 7 struct Student
 8 {
 9     string name;
10     
11     int age;
12     
13     int score;
14 }; 
15 
16 
17 
18 int main(){
19     
20     //结构体数组
21     //语法 struct 结构体名 数组名 [元素个数]={{},{},...} 
22     //2.创建结构体数组
23     struct Student stuArray[8]=
24     {
25         {"张三",18,100},
26         {"李四",28,99},
27         {"王五",38,66},
28     };
29 
30     //3.给结构体数组中的元素赋值
31     stuArray[2].name = "赵六";
32     stuArray[2].age = 80;
33     stuArray[2].score=60; 
34 
35     //4.遍历结构体中元素 
36     for(int i= 0;i<3;i++){
37         cout << "姓名:" << stuArray[i].name 
38             << " 年龄:" << stuArray[i].age
39             <<" 分数:" << stuArray[i].score <<endl;
40     }
41     
42     system("pause");
43     
44     return 0;
45 } 
结构体与数组
 1 #include<iostream>
 2 using namespace std;
 3 #include<string>
 4 //结构体指针 
 5 //定义结构体
 6 struct Student
 7 {
 8     string name;
 9     
10     int age;
11     
12     int score;
13 }; 
14 
15 
16 
17 int main(){
18     
19     //1.创建学生结构体变量
20     struct Student s =
21     {
22         "张三",18,100 
23     };
24     
25     //2.通过指针指向结构体变量
26     
27     struct Student *p =&s;
28     
29     //3.通过指针访问结构体变量中的数据
30     // 通过结构体指针访问结构体中的属性,使用-> 
31         cout <<"姓名:"<< p->name 
32              <<"  年龄:" << p->age 
33              <<"  分数: " << p->score <<endl; 
34     
35     system("pause");
36     
37     return 0;
38 } 
结构体与指针
 1 #include<iostream>
 2 using namespace std;
 3 #include<string>
 4 
 5 //定义学生结构体 
 6 struct Student
 7 {
 8     string name;
 9     int age;
10     int score;
11 }; 
12 //定义老师结构体
13 struct teacher
14 {
15     int id;//教师编号 
16     string name;//教师姓名 
17     int age;//教师年龄 
18     struct Student stu;//辅导的学生 
19 } ;
20 
21 
22 
23 
24 
25 int main(){
26     //结构体嵌套结构体 
27     //创建老师
28     teacher t;
29     t.id=10000;
30     t.name="老王";
31     t.age=50;
32     t.stu.name = "小王";
33     t.stu.age = 20;
34     t.stu.score = 60; 
35     
36     cout <<"教师姓名:"<< t.name 
37         <<"教师编号:" << t.id
38         <<"  教师年龄:" << t.age 
39         <<"\n" 
40         <<"教师辅导学生的姓名: " << t.stu.name 
41         <<"  学生年龄:" << t.stu.age
42         <<"  学生分数:" << t.stu.score
43         << endl;
44     system("pause");
45     
46     return 0;
47 } 
结构体嵌套结构体
 1 #include<iostream>
 2 using namespace std;
 3 #include<string>
 4 
 5 //定义结构体
 6 struct Student
 7 {
 8     string name;
 9     int age;
10     int score;
11 }; 
12 // 值传递 
13 void printStudent1(struct Student s)
14 {
15     s.age = 100;
16     cout <<"子函数1中姓名:"<< s.name 
17     <<"  年龄:" << s.age 
18     << "  分数:" << s.score <<endl; 
19 }
20 
21 
22 //地址传递 
23 void printStudent2(struct Student *p) 
24 {
25     p->age = 200;
26     cout <<"子函数2中姓名:"<< p->name 
27     <<"  年龄:" << p->age 
28     << "  分数:" << p->score <<endl; 
29 }
30 
31 int main(){
32     //结构体做函数参数
33     //将学生传入到一个参数中,打印信息 
34     
35     //创建结构体变量
36     struct Student s = {"张三",20,85}; 
37 
38 //    printStudent1(s); 
39     printStudent2(&s);
40     cout <<"姓名:"<< s.name 
41         <<"  年龄:" << s.age 
42         << "  分数:" << s.score <<endl; 
43     system("pause");
44     
45     return 0;
46 } 
结构体作为函数参数
 1 #include<iostream>
 2 using namespace std;
 3 #include<string>
 4 
 5 //const的使用场景 
 6 //定义结构体
 7 struct Student
 8 {
 9     string name;
10     int age;
11     int score;
12 };  
13 
14 //将函数中的形参改为指针,可以减少内存空间
15 //而且不会复制新的副本 
16 void printStudent(const Student *s)
17 {
18 //    s->age =150; 
19 //加入const后,一旦有修改就报错,防止误操作 
20     cout <<"姓名:"<< s->name 
21     <<"  年龄:" << s->age 
22     <<"  分数: " << s->score <<endl; 
23 }
24 
25 int main(){
26     
27     struct Student s = {"张三",15,70};
28     
29     //通过函数打印结构体变量的信息 
30     printStudent(&s);
31     system("pause");
32     
33     return 0;
34 } 
结构体const的使用场景
 1 #include<iostream>
 2 using namespace std;
 3 #include<stdlib.h>
 4 #include<string>
 5 #include<ctime>
 6 
 7 //结构体案例 
 8 /*设计学生和老师的结构体,其中在老师的结构体中
 9 ,有老师姓名和一个存放5名学生的数组作为成员
10 学生的成员有姓名、考试分数,创建数组存放3名老
11 师,通过函数给每个老师及所带的学生赋值
12 最终打印出老师数据以及老师所带的学生数据。*/ 
13 //学生结构体
14 struct Student
15 {
16     string sName;
17     int score;
18 };  
19 //教师结构体定义 
20 struct Teacher
21 {
22     string tName;
23     struct Student sArray[5];
24  };
25 
26 void allocateSpace(struct Teacher tArray[],int len)
27 {
28     string nameSeed = "ABCDE";
29     //为教师赋值
30     for(int i = 0; i<len;i++)
31     {
32         tArray[i].tName = "Teacher_";
33         tArray[i].tName += nameSeed[i];
34         
35         //通过循环给每名老师所带的学生赋值
36          for(int j=0;j<5;j++)
37          {
38              tArray[i].sArray[j].sName = "Student_";
39             tArray[i].sArray[j].sName += nameSeed[j];
40             
41             int random = rand()%61 + 40; //40~100
42             tArray[i].sArray[j].score = random;
43         } 
44      } 
45 }
46 void printinfo(struct Teacher tArray[],int len)
47 {
48     for(int i = 0;i<len;i++)
49     {
50         cout << "老师姓名:" << tArray[i].tName << endl;
51         for(int j=0 ;j<5;j++)
52         {
53             cout << "\t学生姓名:"  << tArray[i].sArray[j].sName 
54                  <<" 考试分数:"  << tArray[i].sArray[j].score
55                  << endl;
56         } 
57     }
58 }
59 int main(){
60     //随机数种子
61     srand((unsigned int)time(NULL)); 
62     
63     //创建三名老师的数组
64     struct Teacher tArray[3];
65     
66     //通过函数给3名老师赋值,并给学生赋值 
67     int len = sizeof(tArray)/sizeof(tArray[0]);
68     allocateSpace(tArray,len);
69     
70     //打印输出 
71     printinfo(tArray,len); 
72 
73 
74     system("pause");
75     
76     return 0;
77 } 
结构体案例1
 1 #include<iostream>
 2 using namespace std;
 3 #include<string>
 4 
 5 //结构体案例2 
 6 //设计人物结构体
 7 struct hero
 8 {
 9     string name;
10     int age;
11     string sex;
12 } ;
13 
14 //冒泡排序,实现年龄升序排列
15 void bubbleSort(struct hero heroArray[],int len)
16 {
17     for(int i = 0;i<len;i++){
18         for(int j=0;j<len-i-1;j++){
19             if(heroArray[j].age > heroArray[j+1].age)
20             {
21             struct hero temp = heroArray[j];
22             heroArray[j] = heroArray[j+1];
23             heroArray[j+1] = temp;
24             }
25         }
26     }    
27 } 
28 void printhero(struct hero heroArray[],int len)
29 {
30     for(int i = 0;i<len;i++)
31     {
32         cout <<"姓名:" <<heroArray[i].name
33             <<" 年龄: " <<heroArray[i].age
34             <<" 性别:  " <<heroArray[i].sex
35             << endl;  
36     }
37 }
38 int main(){
39      
40     
41     //创建数组存放5位人物
42     struct hero heroArray[5] = 
43     {
44         {"刘备",23,""},
45         {"关羽",22,""},
46         {"张飞",20,""},
47         {"赵云",21,""},
48         {"貂蝉",19,""},
49     };
50     int len = sizeof(heroArray)/sizeof(heroArray[0]); 
51     cout << "排序前:" << endl;
52     printhero(heroArray,len);
53     //对数组进行排序,按照年龄进行升序排列
54     bubbleSort(heroArray,len);
55     
56     //打印输出 
57     cout << "排序后:" << endl;
58     printhero(heroArray,len);
59     
60     system("pause");
61     
62     return 0;
63 } 
结构体案例2(冒泡排序)

 

posted @ 2021-02-07 17:42  stratus  阅读(92)  评论(0)    收藏  举报