结构体

结构体也就是一种用户可以自定义的数据类型

例如:

// 创建学生的数据类型,:学生包括(姓名,年龄,分数)

struct Student {

string name;

int age;

intscore;

}

2、通过学生类型创建具体学生(前提是已经创建了学生这个数据类型)

2.1 struct Student s1;

 给s1 树形赋值,通过访问结构体变量重的属性

s1.name = "张三";

s1.age=18;

s1.score = 100;

2.2 struct Student s2={...}//赋值赋初始值

struct Student s2 = {"李四", 19,80};

2.3 在定义结构体时顺便创建结构体变量

在定义的时候

struct Student {

string name;

int age;

intscore;

}s3

s3.name = "王五";

s3.age = 20;

s3.score= 60;

结构体数组

struct 结构体名称 数组名 [元素个数] = {};

1、定义结构体

2、创建结构体数组

struct  Student stuArray[3]={

{"张三",15,90},

{“李四”,17,100},

{“王五”,20,80}

}

3、给结构体赋值

4、遍历结构体

for(int i = 0 ;i<3;i++)

{

cout<<"姓名:"<<stuArray[i].name <<endl;

j

int main 
结构体变量

int main(){
//创建学生结构体变量
  student s ={"张三",18,100};
//2,通过指针指向结构体变量
student * p =&s;//s 为自定义学生的数据类型 ,那么在定义指针的时候,需要使用同样数据类型的结构体指针来接收
//3 通过指针访问结构体变量中的数据 通过->进行访问
cout<< "姓名" <<p->name<<endl;
}
int main 
结构体嵌套结构体
struct teacher {
  int id;
string name;
  int age;
  struct student stu;

}
//学生结构体定义
struct student {
  string
}
原文地址:https://www.cnblogs.com/gjianli/p/15261375.html