结构体
用户自定义的数据类型,允许存储不同的数据类型。
语法:
struct 结构体名 { 结构体成员列表 } ;
struct Student
{
//成员列表
string name; //姓名
int age; //年龄
int score; //分数
};
讲很多数据类型存放到一起,即 一些类型的集合类型。
创建具体变量
法1. struct Student s1;
int main()
{
struct Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << "姓名:" << s1.name << endl;
cout << "年龄:" << s1.age << endl;
cout << "分数:" << s1.score << endl;
system("pause");
return 0;
}
法2. strcut Student s2 = { . . . } ;
int main()
{
struct Student s2 = { "李四" , 19 , 80 };
cout << "姓名:" << s2.name << endl;
cout << "年龄:" << s2.age << endl;
cout << "分数:" << s2.score << endl;
system("pause");
return 0;
}
法3. 在定义结构体时 顺便创建一个结构体变量s3
```cpp
struct Student
{
//成员列表
string name; //姓名
int age; //年龄
int score; //分数
}s3;
int main()
{
s3.name = "王五";
s3.age = 21;
s3.score = 90;
cout << "姓名:" << s3.name << endl;
cout << "年龄:" << s3.age << endl;
cout << "分数:" << s3.score << endl;
system("pause");
return 0;
}
结构体变量在创建中,d,C++中 struct 是可以省略的。 定义时不可以省略。
即:
int main()
{
Student s2 = { "李四" , 19 , 80 }; // 无 struct Student
cout << "姓名:" << s2.name << endl;
cout << "年龄:" << s2.age << endl;
cout << "分数:" << s2.score << endl;
system("pause");
return 0;
}
- 结构体数组
将自定义的结构体放到数组里
strcut 结构体名 数组名[ 元素个数 ] = { { } , { } , … , { } } ;
```cpp
struct Student
{
//成员列表
string name; //姓名
int age; //年龄
int score; //分数
};
int main()
{
struct Student StuArry[3] = //创建结构体数组
{
{"张三" , 18 , 100 },
{"李四", 19, 80 },
{"王五",21,90}
};
StuArry[2].age = 20; //可后面修改数组中的内容
StuArry[2].score = 70;
for (int i = 0; i < 3; i++)
{
cout << "姓名: " << StuArry[i].name //若一行太多可回车保证整齐,不影响代码功能
<< " 年龄: " << StuArry[i].age
<< " 分数: " << StuArry[i].score << endl;
}
system("pause");
return 0;
}
- 结构体指针
创建指针指向结构体变量
struct Student * p = &s1; // 数据类型为 struct Student ps:struct可省略
通过指针访问结构体变量的数据 p -> name
cout <<"姓名: " << p ->name << endl ;
int main()
{
struct Student s = {"张三", 18, 100 };
struct Student* p = &s;
cout << "姓名:" << p->name << endl; //用箭头
cout << "年龄:" << p->age << endl;
cout << "分数:" << p->score << endl;
system("pause");
return 0;
}
-结构体嵌套结构体
struct Student
{
//成员列表
string name; //姓名
int age; //年龄
int score; //分数
};
struct teacher
{
int id;
string name;
int age;
struct Student stu;
};
使用结构体嵌套时,被使用在内部的结构体一方,如上例中的 struct Student 需要在 struct teacher 之前,否则按会在strcut teacher stu;中显示为未定义的数据类型。
主函数中
struct teacher t;
t.id = 1000;
t.name = "张三";
t.age = 40;
t.stu.name = "李四";
t.stu.age = 16;
t.stu.score = 78;
cout << "老师姓名: " << t.name << " 老师编号: "
<< t.id << " 老师年龄: " << t.age<< " 老师所带学生姓名: "
<< t.stu.name << " 学生年龄: " << t.stu.age << " 学生分数: "
<< t.stu.score << endl;
system("pause");
return 0;
}
需要使用两层 “ . ” ,来给予嵌套中的结构体的值。
- 结构体做函数参数
将结构体作为参数向函数传递
值传递
void printStudent1(struct Student stu) //值传递
{
cout << "学生姓名: "<< stu.name << " 年龄: " << stu.age << " 分数: " << stu.score << endl;
}
void printStudent2(struct Student *p) //地址传递
{
cout << "学生姓名:" << p->name << " 学生年龄:" << p->age << " 学生分数:" << p->score <<endl;
}
int main()
{
struct Student stu;
stu.name = "张三";
stu.age = 18;
stu.score = 80;
printStudent1(stu);
printStudent2(&stu);
system("pause");
return 0;
}
值传递时候,形参发生任何改变不会改变实参。
若 给 void printStudent1(struct Student stu) 函数中添加一句 stu.age = 100;
输出的形参age会改变为100。
但是若main函数中有输出的话,age还是为18。
地址传递时,函数中发生改变之后,主函数中的实参也会变化。
地址传递的优势:
使用值传递,若数据量很大时则会复制出一批新的副本,占用很多内存,因为每一个"张三",都会占用一个struct student 类型。
该结构体占36个字节
换为地址传递,可以减少内存占用,指针4字节
- 结构体中 const 的使用方法
防止误操作
void printStudent2(struct Student *p) //地址传递 无const
{
p -> age = 100;
cout << "学生姓名:" << p->name << " 学生年龄:" << p->age << " 学生分数:" << p->score <<endl;
}
因为地址传递时,子函数中对结构体变量进行了改变后,实参也会变化。
可添加const ,防止修改结构体变量内容,编】译器会提醒。
void printStudent2(const struct Student *p) //地址传递 加const