C++ (struct)

C++ struct简介

一、概念
结构体(struct)是由一系列具有相同类型或不同类型的数据构成的数据集合,也叫结构。它就将不同类型的数据存放在一起,作为一个整体进行处理。

结构体在函数中的作用不是简便,其最主要的作用就是封装。封装的好处就是可以再次利用。让使用者不必关心这个是什么,只要根据定义使用就可以了。

结构体的大小不是结构体元素单纯相加就行的,因为我们现在主流的计算机使用的都是32Bit字长的CPU,对这类型的CPU取4个字节的数要比取一个字节要高效,也更方便。所以在结构体中每个成员的首地址都是4的整数倍的话,取数据元素时就会相对更高效,这就是内存对齐的由来。每个特定平台上的编译器都有自己的默认“对齐系数”(也叫对齐模数)。程序员可以通过预编译命令#pragmapack(n),n=1,2,4,8,16来改变这一系数,其中的n就是你要指定的“对齐系数”。

二、规则
1、数据成员对齐规则:结构(struct)(或联合(union))的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员的对齐按照#pragmapack指定的数值和这个数据成员自身长度中,比较小的那个进行。

2、结构(或联合)的整体对齐规则:在数据成员完成各自对齐之后,结构(或联合)本身也要进行对齐,对齐将按照#pragmapack指定的数值和结构(或联合)最大数据成员长度中,比较小的那个进行。

3、结合1、2可推断:当#pragmapack的n值等于或超过所有数据成员长度的时候,这个n值的大小将不产生任何效果。

三、结构体的定义和使用
语法:struct 结构体名 { 结构体成员列表 };

struct 结构体名 变量名
struct 结构体名 变量名 = { 成员1值 , 成员2值…}
定义结构体时创建变量

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string>
 4 
 5 using namespace std;
 6  
 7 //结构体变量创建方式3:定义结构体时顺便创建变量
 8 struct student
 9 {
10      //成员列表
11      string name;
12      int age;
13      int score;   
14 }stu3;
15  
16  
17 int main(){
18    
19     //结构体变量创建方式1: struct 结构体名 变量名
20     struct student stu1;
21  
22     stu1.name = "七喜";
23     stu1.age = 20;
24     stu1.score = 95;
25     cout<<"姓名:"<<stu1.name <<" 年龄:"<<stu1.age <<" 分数:"<<stu1.score <<endl;
26  
27  
28     //创建结构体变量方式2:struct 结构体名 变量名 = { 成员1值 , 成员2值…}
29     struct student stu2 = {"可乐",19,100};
30     cout<< "姓名:"<< stu2.name <<" 年龄:"<<stu2.age<<" 分数:"<<stu2.score<< endl;
31  
32  
33     //结构体变量创建方式3:
34     stu3.name = "雪碧";
35     stu3.age  = 20;
36     stu3.score = 90;
37     cout<<"姓名:"<<stu3.name <<" 年龄:"<<stu3.age<<" 分数:"<<stu3.score<< endl;
38  
39     return 0;
40 }

四、结构体数组

作用:将自定义的结构体放入到数组中方便维护

语法: struct 结构体名 数组名[元素个数] = { { } , { } ,…, { } }

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string>
 4 
 5 using namespace std;
 6  
 7 //结构体变量创建方式3:定义结构体时顺便创建变量
 8 struct student
 9 {
10      //成员列表
11      string name;
12      int age;
13      int score;   
14 };
15 
16 int main(){
17    //结构体数组
18    struct student stus[3] = {{"康师傅",20,95},{"百事",19,100},{"农夫山泉",20,90}};
19    for(int i=0; i < 3; i++){
20       cout<<"姓名:"<<stus[i].name <<" 年龄:"<<stus[i].age<<" 分数:"<<stus[i].score<< endl;
21    }
22  
23     return 0;
24 }

五、结构体指针

. 和-> 区别:

. 来访问结构体成员/属性

-> 来访问其指向的结构体成员/属性

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string>
 4 
 5 using namespace std;
 6  
 7 //结构体变量创建方式3:定义结构体时顺便创建变量
 8 struct student
 9 {
10      //成员列表
11      string name;
12      int age;
13      int score;   
14 };
15  
16  
17 int main(){
18    
19     //结构体变量创建方式1: struct 结构体名 变量名
20     struct student stu1;
21  
22     stu1.name = "七喜";
23     stu1.age = 20;
24     stu1.score = 95;
25     cout<<"姓名:"<<stu1.name <<" 年龄:"<<stu1.age <<" 分数:"<<stu1.score <<endl;
26 
27 
28    //结构体指针
29    struct student *p = &stu1;
30    cout<<"姓名:"<<(*p).name <<" 年龄:"<<(*p).age<<" 分数:"<<(*p).score<< endl;
31    cout<<"姓名:"<< p->name <<" 年龄:"<< p->age<<" 分数:"<< p->score<< endl;
32 
33  
34     return 0;
35 }

六、结构体嵌套

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string>
 4 
 5 using namespace std;
 6  
 7 //结构体变量创建方式3:定义结构体时顺便创建变量
 8 struct student
 9 {
10      //成员列表
11      string name;
12      int age;
13      int score;   
14 };
15 
16 
17 struct teacher
18 {
19     //成员列表
20     int id; //职工编号
21     string name;  //教师姓名
22     int age;   //教师年龄
23     struct student stu; //子结构体 学生
24 };
25 
26  
27  
28 int main(){
29 
30    //结构体嵌套
31    struct teacher t1;
32     t1.id = 10000;
33     t1.name = "老王";
34     t1.age = 40;
35  
36     t1.stu.name = "张三";
37     t1.stu.age = 18;
38     t1.stu.score = 100;
39  
40     cout << " 教师 职工编号: " << t1.id << 
41             " 姓名: " << t1.name << 
42             " 年龄: " << t1.age << endl;
43     
44     cout << " 辅导学员 姓名: " << t1.stu.name << 
45             " 年龄:" << t1.stu.age << 
46             " 考试分数: " << t1.stu.score << endl;
47 
48 
49  
50     return 0;
51 }

七、结构体做函数参数

传递方式包括:值传递、地址传递

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string>
 4 
 5 using namespace std;
 6  
 7 //结构体变量创建方式3:定义结构体时顺便创建变量
 8 struct student
 9 {
10      //成员列表
11      string name;
12      int age;
13      int score;   
14 };
15 
16 //值传递
17 void printStudent1(student stu){
18     stu.age = 28;
19     cout << "子函数中 姓名:" << stu.name << 
20     " 年龄: " << stu.age  << 
21     " 分数:" << stu.score << endl;
22 
23 }
24 
25 
26 //地址传递
27 void printStudent2(student *stu)
28 {
29     stu->age = 28;
30     cout << "子函数中 姓名:" << stu->name << 
31             " 年龄: " << stu->age  << 
32             " 分数:" << stu->score << endl;
33 }
34 
35  
36  
37 int main(){
38    
39     //结构体做函数参数
40     struct student stu1;
41     stu1.name = "七喜";
42     stu1.age = 20;
43     stu1.score = 95;
44     cout<<"姓名:"<<stu1.name <<" 年龄:"<<stu1.age <<" 分数:"<<stu1.score <<endl
45 
46     
47     printStudent1(stu1);
48     printStudent2(&stu1);
49 
50     return 0;
51 }
  • 18
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值