结构体基本概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型,创建结构时不会分配内存,切记结构体的花括号后面要有分号(;)
结构体的定义和使用
语法:struct 结构体名 {结构体成员列表};
结构体定义尽量要在main函数外面,否则其他函数无法调用
通过结构体创建变量的三种方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #include <iostream> #include <string> using namespace std;
struct student {
string name; int age; int score; };
int main() {
struct student s1; s1.name="张三"; s1.age=18; s1.score=100; cout<<"姓名:"<<s1.name<<"\n年龄:"<<s1.age<<"\n分数:"<<s1.score<<endl; cout<<endl; struct student s2={"李四",18,90}; cout<<"姓名:"<<s2.name<<"\n年龄:"<<s2.age<<"\n分数:"<<s2.score<<endl;
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <iostream> #include <string> using namespace std; struct student {
string name; int age; int score; }s3; int main() {
s3.name="王五"; s3.age=20; s3.score=80; cout<<"姓名:"<<s3.name<<"\n年龄:"<<s3.age<<"\n分数:"<<s3.score<<endl;
return 0; }
|
创建变量时的struct可省
结构体函数
将结构体变量传递给函数并以普通自变量类似的方式返回
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include<iostream> #include<string> using namespace std;
struct person { string name; int age; };
void show(person s) { cout<<s.name<<endl; cout<<s.age<<endl; } int main() { person s1; s1.name="Harbeter"; s1.age=18; show(s1); return 0; }
|
结构体数组
作用:将定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名[元素个数]={{},{}...{}}
这里插一句题外话,hexo发布文件时,这篇老是上不去,最后发现是花括号里不能有花括号,除非变成代码块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include <iostream> #include <string> using namespace std; struct student { string name; int age; int score; }; int main() { struct student stuarr[] { {"张三",18,100}, {"李四",19,90}, {"王五",20,80} }; stuarr[1].name="李8"; for(int i=0;i<3;i++) { cout<<"name:"<<stuarr[i].name<<"\nage:"<<stuarr[i].age<<"\nscore:"<<stuarr[i].score<<endl<<endl; } return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include<iostream> #include<string> using namespace std;
struct person //定义结构体 { string name; int age; }; show(person s) { cout<<s.name<<endl; cout<<s.age<<endl; } int main() { person class3[3]; for(int i=0;i<3;i++) { cin>>class3[i].name>>class3[i].age; } for(int i=0;i<3;i++) { show(class3[i]); } return 0; }
|
结构体指针
作用:通过指针访问结构体中的成员
指针变量不仅可以为本机类型(int、float、double等)创建,还可以为用户定义的类型(如结构体)创建。
利用操作符->
可以通过结构体指针访问结构体属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <iostream> #include <string> using namespace std; struct student { string name; int age; int score; }; int main() { struct student s={"张三",18,100}; struct student *p =&s; cout<<"name:"<<p->name<<"\nage:"<<p->age<<"\nscore:"<<p->score<<endl; cout<<"name:"<<(*p).name<<"\nage:"<<(*p).age<<"\nscore:"<<(*p).score<<endl; return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include<iostream> #include<string> using namespace std;
struct person { string name; int age; }; show(person *s) { cout<<(*s).name<<endl; cout<<(*s).age<<endl; } int main() { person class3[3]; for(int i=0;i<3;i++) { cin>>class3[i].name>>class3[i].age; } for(int i=0;i<3;i++) { show(&class3[i]); } return 0; }
|
忘了三种传参方式移步这
结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
例如:每个老师辅导一个学生,一个老师的结构体中,记录一个学生的结构体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <iostream> #include <string> using namespace std; struct student { string name; int age; int score; };
struct teacher { string name; int age; struct student stu; }; int main() { teacher t; t.name="老王"; t.age=60; t.stu.name="小王"; t.stu.age=20; t.stu.score=100;
cout<<"teacher's name:"<<t.name<<"\nteacher's age:"<<t.age <<"\nteacher's student's name:"<<t.stu.name<<"\nteacher's student's age"<<t.stu.age <<"\nteacher's student's score:"<<t.stu.score<<endl; return 0; }
|