结构体
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
结构体定义、使用
语法:struct 结构体名{结构体成员列表};
创建变量的方式:
①struct 结构体名 变量名
②struct 结构体名 变量名={成员1值,成员2值…}
③定义结构体时顺便创建变量
如定义一个“学生”的数据类型:
struct student{string name; int age; int score;}//“学生”数据类型包括姓名、年龄、分数
由此可见自定义的数据类型就是常用类型集合组成的
创建变量方式:
//①通过.访问结构体变量中的属性
struct student s1; s1.name="张三"; s1.age=18;
//②
struct student s2={"李四", 19, 80};
//③,这个直接忽略,一般用不到
struct student:数据类型,类似于int
s1,s2:变量,类似于a
创建变量时struct可省略,定义时不能省略
结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名[元素个数]={{},{},{}…}
//1.定义结构体数据类型
struct studen{string name; int age; int score;};
//2.创建结构体数组
struct student arr[3]=
{{"张三", 18, 100},
{"李四", 19, 90},
{"王五", 20, 66}};
//3.给结构体数组中的元素赋值
arr[2].name="赵六";//访问数组中第3个元素的name即"王五"->"赵六"
//4.遍历结构体数组
for(int i=0;i<3;i++){
cout<<"姓名:"<<arr[i].name<<" 年龄:"<<arr[i].age<<" 分数:"<<arr[i].score<<endl;
结构体指针
通过指针访问结构体中的成员
利用操作符->可以通过结构体指针访问结构体属性
struct student{string name; int age;}
student s={"张三", 18};
student* p = &s;//通过指针指向结构体变量
//通过指针访问结构体变量中的数据
cout<<p->name<<p->age<<endl;
结构体套结构体
作用:结构体中的成员可以是另一个结构体
struct student{string name; int age; int score};
struct teacher{int id; string name; student s};
//teacher结构体中嵌套student结构体,只需把s看成一个变量即可
//嵌套结构体的外层结构体的定义顺序要在内层之后
//创建老师变量
teacher t;
t.id=0100; t.name="老王"; t.s.name="小王"; t.s.age=20; t.s.score=66;
teacher t2={0101, "老八", {"小八", 18, 88}};
cout<<t.id<<t.s.name<<endl;
结构体作函数参数
作用:将结构体作为参数向函数中传递
传递方式:值传递,地址传递
struct student {
string name;
int age;
int score;
};
//值传递
int main() {
void myPrint(student s);
student s = { "张三", 18, 99 };
myPrint(s);
cout<< "主函数中" << s.name << s.age << s.score << endl;
system("pause");
return 0;
}
void myPrint(student s) {
s.age = 22;
cout << "子函数中" << s.name << s.age << s.score << endl;
}
//地址传递
int main() {
void myPrint(student* p);
student s = { "张三", 18, 99 };
myPrint(&s);
cout<< "主函数中" << s.name << s.age << s.score << endl;
system("pause");
return 0;
}
void myPrint(student* p) {
p->age = 22;
cout << "子函数中" << p->name << p->age << p->score << endl;
}
结构体中const使用场景
作用:用cosnt来防止误操作
void myPrint(const student* p)//加入const之后,一旦有s->name=...等
//修改操作,就会报错,可以仿真我们的误操作。
//为什么不用值传递呢?因为地址传递可以将形参改为指针,可以直接进行传递
//而值传递需要复制出一份新的副本出来再把副本传递进去,因此可占内存空间
//而地址传递可以节省内存空间
{p->age=150;//出错,不可以修改
cout<<p->name<<p->age<<p->score<<endl;}
通讯录管理系统
编写大程序要先构建框架,再丰富细节
子函数中若有system("pause");
则只是结束了子函数,主函数并没有结束
system("pause");
按任意键继续
system("cls");
清屏
在switch中,如果case下面的代码很多,会出错,需把这些代码加上大括号{},形成一个代码块
//在子函数A中,若main函数是地址传递,则接收指针
void main(){
A(&a);
}
void A(int* a){}
//此时,若在子函数A中再调用子函数B,若还是地址传递,则不再用&,因为此时a就已经是指针了
void A(int* a){
B(a);//若写为B(&a);则出错
}
void B(int a){}