I.基本概念
用户自定义数据类型,可存储不同的数据类型
II.定义及使用
语法:
struct 结构体名 {
结构体成员列表
};
注意位于主函数之前
通过结构体创建变量方式:
#include<iostream>
#include<string>
using namespace std;
struct Student
{
string name;
int age;
int score;
}stu3; //方法3:定义结构体时顺便创建变量,用得少
int main(){
//方法1:struct 结构体名 变量名;
struct Student stu1; //struct 关键字可以省略
stu1.name = "张三";
stu1.age = 18;
stu1.score = 100; //通过“.”访问结构体变量属性
cout << "姓名:" << stu1.name << " 年龄: " << stu1.age << " 分数: " << stu1.score << endl;
//方法2:struct 结构体名 变量名={变量1,变量2,···};
struct Student stu2 = { "李四",19,60 }; //struct 关键字可以省略,visual c++ 6.0貌似不支持?
cout << "姓名:" << stu2.name << " 年龄: " << stu2.age << " 分数: " << stu2.score << endl;
//方法3:
stu3.name = "王五";
stu3.age = 18;
stu3.score = 80;
cout << "姓名:" << stu3.name << " 年龄: " << stu3.age << " 分数:" << stu3.score << endl;
system("pause");
return 0;
}
III.结构体数组
作用:将自定义的结构体放入数组中方便维护
语法:
struct 结构体名 数组名[元素个数]={{},{},{},···};
实例:
#include<iostream>
#include<string>
using namespace std;
//1.定义结构体
struct Student
{
string name;
int age;
int score;
};
int main(){
//2.创建结构体数组
struct Student stu[3]={
{"张三",18,100},
{"李四",28,90},
{"王五",38,80}
};
//3.结构体数组中元素赋值
stu[2].name="赵六";
stu[2].age=80;
stu[2].score=60;
//4.遍历结构体数组
for(int i=0;i<3;i++){
cout<<"姓名"<<stu[i].name
<<"年龄"<<stu[i].age
<<"分数"<<stu[i].score<<endl;
}
return 0;
}
IV.结构体指针
作用:通过指针访问结构体成员
利用操作符“->”或“(*p). ”可通过结构体指针访问结构体属性
实例:
#include<iostream>
#include<string>
using namespace std;
//定义结构体
struct Student
{
string name;
int age;
int score;
};
int main(){
//1.创建结构体变量
struct Student stu;
stu.name="张三";
stu.age=18;
stu.score=100;
//2.通过指针指向结构体变量
struct Student *p=&stu; //struct关键字可省略
//3.通过指针访问结构体变量中数据
cout<<"姓名:"<<p->name
<<" 年龄:"<<(*p).age
<<" 分数:"<<p->score<<endl;
return 0;
}
V.结构体嵌套结构体
作用:结构体成员可以是另一个结构体
例如:
#include<iostream>
#include<string>
using namespace std;
//定义学生结构体
struct Student
{
string name; //学生姓名
int age; //学生年龄
int score; //考试分数
};
//定义老师结构体
struct Teacher
{
int id; //教师编号
string name; //教师姓名
int age; //教师年龄
struct Student stu; //辅导的学生
};
int main(){
//创建老师结构体变量
struct Teacher t;
t.id=10001;
t.name="张三";
t.age=58;
t.stu.name="李四";
t.stu.age=20;
t.stu.score=94;
cout<<"老师姓名:"<<t.name<<" 老师编号:"<<t.id<<" 老师年龄:"<<t.age
<<" 老师辅导的学生姓名:"<<t.stu.name<<" 学生年龄:"<<t.stu.age
<<" 学生成绩:"<<t.stu.score<<endl;
return 0;
}
VI.结构体做函数参数
传递方式:值传递,地址传递
值传递
#include<iostream>
#include<string>
using namespace std;
//定义学生结构体
struct Student
{
string name; //学生姓名
int age; //学生年龄
int score; //考试分数
};
//打印学生信息函数
//1.值传递
void printStudent1(struct Student stu){
stu.age=100;
cout<<"值传递子函数1中打印 姓名:"<<stu.name<<" 年龄:"<<stu.age<<" 考试分数:"<<stu.score<<endl;
}
//2.地址传递
void printStudent2(struct Student *p){
p->age=120;
cout<<"地址传递子函数2中打印 姓名:"<<p->name<<" 年龄:"<<(*p).age<<" 考试分数:"<<p->score<<endl;
}
int main(){
//创建结构体变量
struct Student stu;
stu.name="张三";
stu.age=18;
stu.score=100;
printStudent1(stu);
//printStudent2(&stu);
cout<<"在main函数中打印年龄:"<<stu.age<<endl;
return 0;
}
注意到实参并未随形参改变而改变!
地址传递
#include<iostream>
#include<string>
using namespace std;
//定义学生结构体
struct Student
{
string name; //学生姓名
int age; //学生年龄
int score; //考试分数
};
//打印学生信息函数
//1.值传递
void printStudent1(struct Student stu){
stu.age=100;
cout<<"值传递子函数1中打印 姓名:"<<stu.name<<" 年龄:"<<stu.age<<" 考试分数:"<<stu.score<<endl;
}
//2.地址传递
void printStudent2(struct Student *p){
p->age=120;
cout<<"地址传递子函数2中打印 姓名:"<<p->name<<" 年龄:"<<(*p).age<<" 考试分数:"<<p->score<<endl;
}
int main(){
//创建结构体变量
struct Student stu;
stu.name="张三";
stu.age=18;
stu.score=100;
//printStudent1(stu);
printStudent2(&stu);
cout<<"在main函数中打印年龄:"<<stu.age<<endl;
return 0;
}
注意到由于指针指向实参地址,所以形参实参均改变!
函数中形参为指针,只接受结构体地址而不会像值传递一样复制新的副本,可减少内存空间!
VII.结构体中const应用
作用:防止误操作
注意到地址传递时如果在函数中误操作修改了某属性,则主函数中实参也会改变。如果想避免这种情况,可考虑在函数中定义常量指针 const struct Student *p,则一旦有修改操作就会报错。
void printStudent2(const struct Student *p){
p->age=120; //非法操作,会报错
cout<<"地址传递子函数2中打印 姓名:"<<p->name<<" 年龄:"<<(*p).age<<" 考试分数:"<<p->score<<endl;
}