struct 基本应用方法 及 题目常用使用形式详解

本文介绍了如何使用C++的struct定义结构体来组合学生的基本信息,包括姓名、性别和学号,并展示了两种常见的定义方法。结构体在BFS和DFS中的应用,如存储运动方向和队列节点,以及在链表和游戏开发中的角色,都被详细阐述。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

结构体的定义:结构体是由一批数据组合而成的结构型数据。组成结构型数据的每个数据称为结构型数据的“成员”,其描述了一块内存区间的大小及解释意义。


我们在解决实际问题的时候,经常会遇见用几个数据类型来表示某个变量。

例如我们在用变量来表示一个学生的时候,会用到他的(字符串)姓名,(字符串)性别,(整型)学号

那我们要怎么集合这些学生的信息

形成一个整体学生呢

这个时候就用到了结构体struct

struct可以将你需要来表示某个变量的信息集合在一起

将结构体封装好以后就可以重复使用

让我们来看一下如何封装一个学生的结构体

struct student//学生
{
	string name;//姓名
	string sex;//性别
	int ID;//学号
};

结构体封装好了

那我们要怎么才能对封装好的结构体进行使用呢

结构体常用的定义的方法:

1.直接在结构体后面进行定义

struct student//学生
{
	string name;//姓名
	string sex;//性别
	int ID;//学号
}s1={"勇裴","男",1234567890},s2={"逊宾","男",1234567891};

2.用struct构成局部变量

struct student//学生
{
	string name;//姓名
	string sex;//性别
	int ID;//学号
};
int main()
{
    student s1={"勇裴","男",1234567890},s2={"逊宾","男",1234567891};
    return 0;
}

我们来看一下他们的输出结果(方式2在平时生活使用较为广泛,但他们最终的效果都一样)

#include<iostream>
using namespace std;
#include<string>
struct student//学生
{
	string name;//姓名
	string sex;//性别
	int ID;//学号
};
int main()
{
	student s1 = { "勇裴","男",1234567890 }, s2 = { "逊宾","男",1234567891 };
	cout << s1.name << " " << s1.sex << " " << s1.ID << endl;
	cout << s2.name << " " << s2.sex << " " << s2.ID << endl;
	return 0;
}

 输出结果:


效果很明显

我们通过结构体

轻松的将一个学生的基本信息结合到了一起

那我们在做题的时候会在哪些方面用到结构体呢

博主在这里介绍几种常用的结构体的使用场景

1.在进行bfs和dfs的时候

1)在做深度优先搜索(dfs)题的时候

通常我们进行dfs 的时候可能会碰到

需要进行上下左右的移动

这个时候我们就可以用

struct来存我们要走的方向

例如:

struct direction
{
	int x;//代表第几行
	int y;//代表第几列
};

一般使用模板:

struct direction
{
	int x;//代表第几行
	int y;//代表第几列
}around[] = { {0,1} ,{1,0},{0,-1},{-1,0} };

for (int i = 0; i < 4; i++)//分别走四个方向
{
	int fx = nowx + around[i].x;
	int fy = nowy + around[i].y;
	if (x > n || y > m || x < 1 || y < 1) continue;//超出所给地方的边界
	if (......) continue;//......代表是什么不满足条件的走法(如该地方有障碍物不能走)
	vis[fx][fy] = 1;//记录该位置走过
	dfs(fx, fy);//继续dfs
}

2)在做bfs的时候:

我们除了可以和做dfs一样用来当做运动方向

还可以用来存队列queue里面的位置

一般查找时使用模板:

struct direction
{
	int x;//代表第几行
	int y;//代表第几列
}around[] = { {0,1} ,{1,0},{0,-1},{-1,0} };

struct node
{
	int x;
	int y;
};
for (int i = 0; i < 4; i++)
{
	node t;
	int t.x = nowx + around[i].x;
	int t.y = nowy + around[i].y;
	if (x > n || y > m || x < 1 || y < 1) continue;//超出所给地方的边界
	if (......) continue;//......代表是什么不满足条件的走法(如该地方有障碍物不能走)
	vis[fx][fy] = 1;//记录该位置走过
	q.push(t);//如果满足条件那么将t放入队列里面
}

2.链表

对链表进行连接的时候

用一个结构体连接下一个结构体

构成链表

struct Node {
	int data;
	struct Node * nextNode; //指针
};

恭喜!你已经成功对struct了解了常用的用法了,加油!!!

PS:今人不见古时月,今月曾经照古人。

#include"stdio.h" #include"stdlib.h" #define NULL 0 struct student { long num; char name[20]; int score[6]; struct student *next; }; void show() { printf("\nthere is a cataloge as follow.\n"); printf("***************************************\n"); printf("* *\n"); printf("* 1. create *\n"); printf("* 2. Insert *\n"); printf("* 3. print *\n"); printf("* 4. delete *\n"); printf("* 5. modify *\n"); printf("* 6. save_to_file *\n"); printf("* 7. lode from file *\n"); printf("* 8. exit *\n"); printf("***************************************\n"); printf("please input 1--8 to choice what you want:\n"); } struct student *create() { struct student *head,*p,*last; int i; int temp2; char name[20]; p=head=(struct student *)malloc(sizeof(struct student)); head->next=NULL; last=p; while(1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); /*last->next=p; last=p;*/ p->next=NULL; printf("number:"); scanf("%ld",&p->num); if(p->num==0)break; getchar(); printf("name:"); scanf("%s",p->name); printf("score:"); for(i=0;iscore[i]=temp2; } printf("next student's information.\n"); } free(p); return head; } void Insert(struct student *head) { struct student *p,*q; int score; long num; int i; printf("\nEnter the student's information you want to insert.\n"); printf("number:"); scanf("%ld",&num); q->num=num; getchar(); printf("name:"); scanf("%s",q->name); printf("score:(chinese,math,english,biology,physics,chemistry)\n"); for(i=0;iscore[i]=score; } q->next=NULL; p=head; while(p->next->numnum&&p->next!=NULL) p=p->next; q->next=p->next; p->next=q; if(p->next==NULL) p->next=q; } void delete(struct student *head) { struct student *p,*q; long num; printf("enter the student's information you want to delete.\n"); printf("number:"); scanf("%ld",&num); getchar(); p=head; while(p->next!=NULL&&p->next->num!=num) p=p->next; q=p->next; p->next=p->next->next; free(q); } void print(struct student *head) { struct student *p; int i; p=head->next; if(p==NULL) { printf("\nthere is no information.\n"); exit(0); } printf("\nnumber\tnamme\tchinese\tmath\tenglish\tbiology\tphysics\tchemistry\n"); while(p!=NULL) { printf("\n%ld\t%s",p->num,p->name); for(i=0;iscore[i]); p=p->next; } } void modify(struct student *head) { struct student *p; int choice,i; long num; char name[20]; int score[6]; printf("\nEnter what student's information you want to modify.\n"); printf("number:"); scanf("%ld",&num); getchar(); printf("\nname:"); scanf("%s",name); printf("\n"); p=head->next; while(p->num!=num&&p->name[20]!=name[20]&&p!=NULL) p=p->next; printf("\nplease choice what you want to modify:1-number 2-name 3-score.\n"); scanf("%d",&choice); switch(choice) { case 1:printf("\nEnter the true number:"); scanf("%ld",&num); p->num=num; break; case 2:printf("\nEnter the true name:"); scanf("%s",p->name); break; case 3:printf("\nEnter the right score:"); for(i=0;iscore[i]=score[i]; } break; } } void save_in(struct student *head) { struct student *p; FILE *fp; char file_name[30]; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"w"))==NULL) { printf("can't open the file.\n"); exit(0); } p=head; while(p->next!=NULL) { fwrite((void*)p->next,sizeof(struct student),1,fp); p=p->next; } fclose(fp); } struct student *load_from_file() { struct student *head,*p,*last; FILE *fp; char file_name[30]; head=(struct student *)malloc(sizeof(struct student)); last=head; head->next=NULL; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"r"))==NULL) { printf("can't open the file.\n"); exit(0); } p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; while(fp=fread((void *)p,sizeof(struct student),1,fp)==1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; } free(p); fclose(fp); return head; } void main() { struct student *la; int choice; /*char Yes_No;*/ la=(struct student *)malloc(sizeof(struct student)); la->next=NULL; while(1) { show(); scanf("%d",&choice); switch(choice) { case 1:la=create(); break; case 2:Insert(la); break; case 3:print(la); break; case 4:delete(la); break; case 5:modify(la); break; case 6:save_in(la); break; case 7:la=load_from_file(); break; case 8:exit(0); } } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XDU-Yoghurt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值