//通过指针来修改struct 结构体的属性值以及打印等操作:
struct Student s1 = {"张三",10,30};
struct Student * p = &s1;
cout << "名字:" <<p->name <<",年龄:" << p->age << ",成绩:" << p->scores << endl;
//结构体嵌套结构体
struct Student{
string name;
int age;
int scores;
}; // 这里定义的时候就创建了student 变量。
struct Teacher{
int id;
string name;
int age;
struct Student student;
};
struct Student s1 = {"张三",10,30};
struct Teacher teacher = {1,"王老师",40,s1};
struct Teacher * teacherPointer = &teacher;
cout << "id:" <<teacherPointer->id <<",年龄:" << teacherPointer->age << ",学生:" << teacherPointer->student.name << endl;
//结构体变量成为形参的时候,建议使用指针,节约内存占用。内存仅有4个字节或者8个字节。结构体中的数据如果copy 一份的话,会多占用内存。使用地址(指针)传递的时候,建议用const修饰。
srand种子的时候,需要引入#include<ctime>