C++中的结构体

定义:结构体属于用户自定义的类型,允许用户存储不同的数据类型。

语法:struct 结构体名{结构体成员列表};

通过结构体创建变量我们这里采用5种方式,其中,前4种方式本质相同

1、声明结构体 2、创建结构体 3、初始化

1、按顺序(声明--创建--初始化)

#include <iostream>
using namespace std;
//声明结构体
struct Student 
{
    string name;
    int age;
    float score;
};

int main()
 {    
    struct Student s1;
    s1.name = "tom";
    s1.age = 12;
    s1.score = 99;
    cout << "name:" << s1.name << "age:" << s1.age << "score:" << s1.score << endl;
    return 0;
}

2、使用关键字typedef,省略struct关键字

#include <iostream>
using namespace std;
//声明结构体
typedef struct Student {
    string name;
    int age;
    float score;
};

int main() {
    //创建的时候struct关键字可以省略
    Student s1;
    s1.name = "tom";
    s1.age = 12;
    s1.score = 99;
    cout << "name:" << s1.name << "age:" << s1.age << "score:" << s1.score << endl;
    return 0;
}

在我使用codeblocks时,是不用添加typedef关键字的,当我添加此关键字时,编译警告.但是我们使用C语言或者其他编译器时,是需要添加的,所以此处,我们需要了解一下(以下程序我们将不考虑此情况).

typedef用途:用来给现有的数据类型定义同义词的。

参考资料:c【转】C++ typedef用法小结_destiny若小羽的博客-CSDN博客

2.创建和初始化同时进行

#include <iostream>
using namespace std;
//声明结构体
struct Student {
    string name;
    int age;
    float score;
};

int main()
 {
    struct Student s2 ={"jack", 15, 98};
    cout << "name:" << s2.name << "age:" << s2.age << "score:" << s2.score << endl;
    return 0;
}

3.声明和创建同时进行

#include <iostream>
using namespace std;
struct Student {
    string name;
    int age;
    float score;
}s3;

int main()
 {
    s3.name = "bob";
    s3.age = 19;
    s3.score = 97;
    cout << "name:" << s3.name << "age:" << s3.age << "score:" << s3.score << endl;
    return 0;
}

4.声明,创建,初始化同时进行

#include <iostream>
using namespace std;
struct Student {
    string name;
    int age;
    float score;
}s3={
      "bob",
     19,
    97,
};

int main()
 {
    cout << "name:" << s3.name << "age:" << s3.age << "score:" << s3.score << endl;
    return 0;
}

1-2-3

12-3

1-23

123

感觉挺有意思的,只要保证顺序不变,怎么结合都是无所谓的

5.匿名结构体,在定义时同时声明2个结构体变量,但不能在其它地方声明,因为我们无法得知该结构体的标识符,所以就无法通过标识符来声明变量。

#include<stdio.h>
struct {
    int sNo;
    char name[10];
}stu,stu1;     //匿名结构体,同时定义了2个结构体变量
int main(){
    scanf("%d",&stu.sNo);
    scanf("%s",stu.name);
    printf("%d\n",stu.sNo);
    scanf("%d",&stu1.sNo);
    scanf("%s",stu1.name);
    printf("%d\n",stu1.sNo);
}

总结1:定义结构体时关键字是struct,不可省略

总结2:创建结构体变量时,关键字struct可以省略(注意上面我们叙述过原因)

总结3:结构体变量使用操作符"."访问成员

 结构体数组

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

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

#include <iostream>
using namespace std;

struct Student {
    string name;
    int age;
    float score;
}s3[]={
      {"bob",19,97,},
      {"ooa",20,88}
};

int main()
 {
     int i;
     
   for(i=0;i<2;i++)
    {
        cout << "name:" << s3[i].name << "age:" << s3[i].age << "score:" << s3[i].score << endl;
    }
    
    return 0;
}





#include <iostream>
using namespace std;

struct Student {
    string name;
    int age;
    float score;
};



int main()
 {
     int i;
    Student s3[]={
                    {"bob",19,97,},
                    {"ooa",20,88}
                 };

   for(i=0;i<2;i++)
    {
        cout << "name:" << s3[i].name << "age:" << s3[i].age << "score:" << s3[i].score << endl;
    }

    return 0;
}





#include <iostream>
using namespace std;

struct Student {
    string name;
    int age;
    float score;
}s3[2];



int main()
 {
     int i;
    s3[0].name="bob";
    s3[0].age=19;
    s3[0].score=97;
    s3[1].name="ooa";
    s3[1].age=20;
    s3[1].score=88;


   for(i=0;i<2;i++)
    {
        cout << "name:" << s3[i].name << "age:" << s3[i].age << "score:" << s3[i].score << endl;
    }

    return 0;
}




#include <iostream>
using namespace std;

struct Student {
    string name;
    int age;
    float score;
};



int main()
 {
     int i;
     Student s3[2];
    s3[0].name="bob";
    s3[0].age=19;
    s3[0].score=97;
    s3[1].name="ooa";
    s3[1].age=20;
    s3[1].score=88;


   for(i=0;i<2;i++)
    {
        cout << "name:" << s3[i].name << "age:" << s3[i].age << "score:" << s3[i].score << endl;
    }

    return 0;
}

结构体指针

作用:通过指针访问结构体中的成员

  • 利用操作符 ->可以通过结构体指针访问结构体属
#include <iostream>
using namespace std;
struct Student {
    string name;
    int age;
    float score;
};


int main()
 {
    struct Student s = { "tom",12,99 };
    //定义一个结构体指针
    Student* p = &s;
    //需要使用->来访问
    cout << p->name << p->age << p->score << endl;
    return 0;
}

四、结构体嵌套结构体

作用:结构体中的成员可以是另一个结构体

例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体

#include<iostream>

//** 例如:** 每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体

using namespace std;
//全局变量声明区

//函数声明区

//学生结构体
struct student
{
	//成员列表
	string name;		//姓名
	int age;			//年龄
	int score;			//分数
};

//教师结构体定义
struct teacher
{
	//成员列表
	int id;				//职工编号
	string name;		//教师姓名
	int age;			//教师年龄
	struct student stu; //子结构体 学生
};

int main()
{
	struct teacher t1;
	t1.id = 10000;
	t1.name = "老王";
	t1.age = 40;

	t1.stu.name = "张三";
	t1.stu.age = 18;
	t1.stu.score = 100;

	cout << "教师 职工编号:" << t1.id << " 姓名: " << t1.name << " 年龄: " << t1.age << endl;
    cout << "辅导学员 姓名:" << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分数: " << t1.stu.score << endl;
    return 0;
}

总结:在结构体中可以定义另一个结构体作为成员,用来解决实际问题.

结构体做函数参数

作用:将结构体作为参数向函数中传递

传递的方式有三种:

  • 值传递
  • 地址传递
  • 引用传递
#include<iostream>
using namespace std;
//全局变量声明区

//学生结构体定义
struct student
{
	string name;
	int age;
	int score;
};
//函数声明
void printfStudent(student x);
void printfStudent2(student* x);
void printfStudent3(student &x);

int main()
{
	student stu = { "张三", 18, 100 };
	//值传递
	printfStudent(stu);
	cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;

	//地址传递
	printfStudent2(&stu);
	cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
	//引用传递

	printfStudent3(stu);
	cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
	return 0;
}

//值传递
void printfStudent(student x)
{
	x.age = 28;
	cout << "子函数中 姓名:" << x.name << " 年龄: " << x.age << " 分数:" << x.score << endl;
}

//地址传递
void printfStudent2(student* x)
{
	x->age = 28;
	cout << "子函数中 姓名:" << x->name << " 年龄: " << x->age << " 分数:" << x->score << endl;
}

void printfStudent3(student &x)
{
  x.age=28;
  cout << "子函数中 姓名:" << x.name << " 年龄: " << x.age << " 分数:" << x.score << endl;
}

 六、结构体中const的使用场景

作用:用const防止误操作 

#include <iostream>
using namespace std;

struct Student {
    string name;
    int age;
    float score;
};

//值传递
void printStudent(const Student* stu) {
    /*这里就不能进行修改了
    stu->name = "jack";
    stu->age = 22;
    stu->score = 90;
    */
    cout << "结构体中的stu信息:" << endl;
    cout << stu->name << "," << stu->age << "," << stu->score << endl;
}


int main() {
    struct Student stu = { "tom",12,99 };
    struct Student* p = &stu;
    cout << "未传入函数之前的结构体值:" << endl;
    cout << stu.name << "," << stu.age << "," << stu.score << endl;
    printStudent(p);
    cout << "传入函数之后的结构体值:" << endl;
    cout << stu.name << "," << stu.age << "," << stu.score << endl;
    return 0;
}

只需要记住,用const修饰的数据类型就不能再进行更改。  

枚举

一、定义 

为了解决const每次只能声明一个符号常量,C++的enum提供l一种大量创建符号常量的方式。

语法:enum EnunName{type1,type2,type3,type4,......}

enum spectrum{red,orange,yellow,green,blue,violet,indigo,ultraviolet};

让spectrum成为新类型的名称,red等作为符号常量,对应整数值为0~7,这些常量叫做枚举量。

二、用法

可以用枚举名声明这种变量,实际上枚举更常被用来定义相关的符号常量,而不是新类型

spectrum band;

如果只是打算只使用常量,而不创建枚举类型的变量,则可以省略枚举类型名称,如下所示

enum {red,orange,yellow,green,blue,violet,indigo,ultraviolet};

1、在不进行强制类型转换的情况下,只能将定义枚举时使用的枚举值赋给这种枚举的变量

band =blue;//valid,blue is an enumerator
band=2000;//invalid,2000 not an enumerator

2、只定义了赋值运算符

band =orange;           //valid
++band;                 //not valid
band=orange+red;        //not valid

3、枚举量是整型,可被提升为int类型,但int类型不能自动转化为枚举类型

int color =blue;   //valid
band=3;            //invalid
color =3+red;      //valid

4、在C++早期版本中,只能将int值(或提升为int的值)赋给枚举量,但这种限制取消了,因此可以使用long甚至long long类型的值。 

三、设置枚举量的值

1、可以使用赋值运算符来显示地设置枚举量的值

enum bits{one=1,two=2,four=4,eight=8};

2、可以只显示地定义其中一些枚举量的值

enum bigstep{first,second=100,third};

这里,first在默认情况下为0。后面没有被初始化的枚举量的值将比其前面的枚举量大1,因此,third的值为101.

3、可以创建多个值相同的枚举量

enum {zero,null=0,one,numero_uno=1};

4、枚举的取值范围

每个枚举都有取值范围,通过强制类型转换,可以将取值范围中任何整数值赋给枚举变量,即使这个值不是枚举值。

enum bits{one=1,two=2,four=4,eight=8};
bits myflag;
myflag=bits(6);

其中6不是枚举值,但它位于枚举定义的取值范围内。

具体范围大小查即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值