我是靠谱客的博主 默默项链,这篇文章主要介绍结构体数组的用法小结,现在分享给大家,希望可以做个参考。

复制代码
1
2
原文章链接(写的相当详细)http://c.biancheng.net/cpp/biancheng/view/172.html

1.结构体的作用:结构体是用来存储多个数据变量时用到的,通过结构体可以实现数据变量在全局范围内的所有函数中的使用。

复制代码
1
2
3
4
5
6
7
8
9
10
11
//例如我们声明结构体类型Student,用来存储一个学生各种各样的数据 struct Student { int num; char name[20]; char sex; int age; float score; char addr[30]; };

但现在问题来了,我们不可能只有一个学生,必定是有很多学生的,这就得使用数组,两这一组合就形成了结构体数组。
结构体数组与以前介绍过的普通数值型数组的不同之处在于:每个数组元素都是一个结构体类型的数据,它们都分别包括各个成员项。

2.有三种不同的写法,下面一一介绍:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
//1. struct Student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; Student stu[3]; //定义Student类型的数组stu
复制代码
1
2
3
4
5
6
7
8
9
10
11
//2. struct Student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu[3]; //定义Student类型的数组stu
复制代码
1
2
3
4
5
6
7
8
9
10
11
//3.甚至连struct后面的名字都可以省去 struct { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu[3]; //定义Student类型的数组stu

3.结构体的初始化

作为一个高素质的程序员,必须养成定义变量要初始化的习惯。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu[3]={ {10101,"Li Lin", 'M', 18,87.5, "103 Beijing Road"}, {10102,"Zhang Fun",'M',19,99, "130 Shanghai Road"}, {10104,"Wang Min",'F', 20,78.5, "1010 Zhongshan Road"} };

定义数组stu时,也可以不指定元素个数,即写成以下形式:
stu[ ]={{…},{…},{…}};
编译时,系统会根据给出初值的结构体常量的个数来确定数组元素的个数。一个结构体常量应包括结构体中全部成员的值。

当然,数组的初始化也可以用以下形式:
Student stu[ ]={{…},{…},{…}}; //已事先声明了结构体类型Student
由上可以看到,结构体数组初始化的一般形式是在所定义的数组名的后面加上 ={初值表列};

4.结构体的易错处:
cout<<stu[i].age; 这种可以,
但cout<<stu[i];这种直接输出一整个结构体的会报错,原因就是类型转换。
在这里插入图片描述
想要输出一整个结构体只能一个变量一个变量的输出

复制代码
1
2
3
4
5
6
7
8
9
for(int i=0;i<3;i++){ cout<<stu[i].addr<<endl; cout<<stu[i].age<<endl; cout<<stu[i].name<<endl; cout<<stu[i].num<<endl; cout<<stu[i].score<<endl; cout<<stu[i].sex<<endl; }

5.结构体存储数据时string和字符数组的效率比较

例题:对候选人得票的统计程序。设有3个候选人,最终只能有1人当选为领导。今有10个人参加投票,从键盘先后输入这10个人所投的候选人的名字,要求最后输出这3个候选人的得票结果。

(i)字符数组

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream> using namespace std; struct Person //声明结构体类型Person { char name[20]; int count; }; int main( ) { //定义Person类型的数组,内容为3个候选人的姓名和当前的得票数 Person leader[3]={"Li",0,"Zhang",0,"Fun",0}; int i,j; char leader_name[20]; //leader_name为投票人所选的人的姓名 for(i=0;i<10;i++) { cin>>leader_name; //先后输入10张票上所写的姓名 for(j=0;j<3;j++) //将票上姓名与3个候选人的姓名比较 //如果与某一候选人的姓名相同,就给他加一票 if(strcmp(leader_name,leader[j].name)==0) leader[j].count++; } cout<<endl; for(i=0;i<3;i++) //输出3个候选人的姓名与最后得票数 { cout<<leader[i].name<<":"<<leader[i].count<<endl; } return 0; }

(ii)string

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream> #include <string> using namespace std; struct Person { string name;//成员name为字符串变量 int count; }; int main( ) { Person leader[3]={"Li",0,"Zhang",0,"Fun",0}; int i,j; string leader_name;// leader_name为字符串变量 for(i=0;i<10;i++) { cin>>leader_name; for(j=0;j<3;j++) if(leader_name==leader[j].name) leader[j].count++//用“==”进行比较 } cout<<endl; for(i=0;i<3;i++) { cout<<leader[i].name<<":"<<leader[i].count<<endl; } return 0; }

总结:运行情况与前相同。显然后一个程序(使用string的)更节省内存空间,使用更方便,易读性更好。但是 有些C++系统不能对包含string成员的结构体变量初始化,需要作一些修改才能运行, 读者可上机试一下。

最后

以上就是默默项链最近收集整理的关于结构体数组的用法小结的全部内容,更多相关结构体数组内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部