Lecture 7 自定义类型
来源:互联网 发布:c语言从1加到100 编辑:程序博客网 时间:2024/06/05 07:22
C++的数据类型
结构体概述
struct Student
{ int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
} ;
向编译系统声明:Student是一种结构体类型,包括num,name,sex,age,score,addr等不同类型数据项
使用了结构体类型的程序:
struct Date{ 结构体类型首字母大写
int year;
int month;
int day;
};
int main(){
Data data;
cout<<"input year,month,day:";
cin>>data.year>>data.month>>data.day;
int days;
.......
cout<<data.month<<"月"<<data.day<<"日是"<<data.year<<"年的第<<days<<"天”<<endl;
return 0;
}
声明一个结构体类型的一般形式
.struct 结构体类型名
{
类型名 成员名
类型名 成员名 (成员列表,又叫做域表)
.....
};
结构体类型变量的定义方法
1 先声明结构体类型再定义变量名
.struct Student
{
.........
};
Student student1,student2;
结构体类型Student只是一种数据类型,不占内存空间,只有定义结构体类型变量student1,student2时才开辟内存空间
2 在声明类型的同时定义变量
.struct Student
{
........
}student1,student2;
3 直接定义结构体类型变量
.struct
{
........
}student1,student2;
结构体类型没有起名字,直接定义变量
提倡第一种方法,一段代码只做一件事,只定义结构体类型
第二种方法既定义结构体类型,又定义了结构体变量
第三种相较第二种,以后不能重复使用,因为没定义类型名
结构体中的成员可以是结构体变量,为结构体类型,与其他数据类型同等看待
结构体变量的引用:一个结构体变量的值赋给另一个具有相同结构的结构体变量
struct Student
{ int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
}stu1={10001,"zhang xin",'m',19,90.5,beijing}
Student stu2;
stu2=stu1;
Stu1在定义时分配一段内存并且初始化,stu2也分配一段内存,之后将stu1赋值给stu2,但所占用的内存不是同一段
结构体变量的引用:结构体变量.成员名
可以引用一个结构体变量中的一个成员的值,如:student1.num=10001;
.的优先级很高,结合性自左向右
例:引用结构体变量中的成员
struct Data
{ int month;
int day;
int year;
};
struct Student
{ int num;
char name[20];
char sex;
Data birthday;
float score;
}stu1,stu2={10001,"zhang xin",'m',5,23,1982,90.5};
int main()
{ stu1=stu2; 赋值相同,但是所在存储单元不同
cout<<stu1.num<<endl;
cout<<stu1.name<<endl;
cout<<stu1.sex<<endl;
cout<<stu1.birthday.month<<'/'<<stu1.birthday.day<<'/'<<stu1.birt hday.year<<endl; 不能直接写为stu1.birthday,因为该数据类型cout不识别
cout<<stu1.score<<endl;
return 0;
}
结构体数组
每个数组元素都是一个结构体类型的数据
struct Student
{ int num;
....
Char addr[30];
};
Student stu[3]; student类型的一维数组,每个元素都是一个结构体,且一个数组中的存储空间是连续的
定义结构体数组与定义结构体变量的方式一样,也有三种,优先选择第一个
结构体数组的初始化:Student stu2[里面可以不写]={{结构体数据},{...},{...}...};
结构体数组应用举例
对候选人得票的统计程序。设有3个候选人,最终只能有1人当选为领导。今有10个人参加投票,从键盘先后输入这10个人所投的候选人名单,要求最后输出这3个候选人的得票结果。
程序:
struct Person
{ char name[20];
int count;
}; 定义结构体类型
int main()
{ int i,j;
Person leader[3]={"li",0,"fun",0,"zhang",0}; 定义结构体数组(可分别加大括号)
char leader_name[20];
for(i=0;i<10;i++)
{ cin>>leader_name;
for(j=0;j<3;j++)
if(strcmp(leader_name,leader[j].name)==0)
leader[j].count++;
}
for(i=0;i<3;i++)
cout<<leader[i].name<<leader[i].count<<endl; 分别输出
}
- Lecture 7 自定义类型
- Lecture 7
- RHCE131 Lecture 7
- CS224d lecture 7札记
- CS109 Lecture 7
- Lecture 7: Designing Specifications
- CS107-Lecture 7-Note
- Lecture 7 Hashing Table I
- MIT 6.006 Algorithm Lecture 7
- 自定义类型
- 自定义类型
- 自定义类型
- Jordan Lecture Note-7: Soft Margin SVM
- Unit 1-Lecture 7:Binary Relation & Function
- Lecture 7 Sigma Delta Converters 积分增量调制
- Oracle使用对象类型7(自定义…
- strtus2学习(7)自定义类型转换器
- spring MVC (7) 定义自定义类型转换器
- H面试程序(14): 快速排序法
- 金融信息交换协议:Fix协议(一)
- C++用CreateProcess来打开IE
- 将STM32的标准库编译成lib【图文】
- HDU 2546 饭卡 (0/1背包问题)
- Lecture 7 自定义类型
- Linux 中make install安装的软件卸载
- Extjs 4.x 中ComboBox级联问题
- java中break,continue和return的区别
- 修复Linux Mint损坏的依赖
- hdu 1525 Euclid's Game
- HDU 2602 Bone Collector (0/1背包问题)
- Linux博文索引
- jQuery zTree v3.5 实例4 对树的节点进行编辑和删除