8 结构体
8.1 结构体基本概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
结构题定义和使用
语法: struct 结构体名{ 结构体成员列表 };
通过结构体创建变量的方式有三种:
- struct 结构体名 变量名
- struct 结构体名 变量名={成员1值,成员2值...}
- 定义结构体时顺便创建变量
示例:
[code]#include#includeusing namespace std;//1、创建学生数据类型;//学生:姓名,年龄,分数//自定义数据类型,一些类型集合组成的一个类型//语法 struct 类型名称 { 成员列表 };struct Student{ //成员列表 //姓名 string name; //年龄 int age; //分数 int score;}s3;//2、通过学生类型创建具体学生//2.1 struct Student s1;//2.2 struct Student s2 = {...}//2.3 在定义结构体时候顺便创建结构体变量int main(){ //2.1 struct Student s1; //struct 关键字可以省略 struct Student s1; s1.name = "张三"; s1.age = 18; s1.score = 100; cout |