1. 结构体基本概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
2. 结构体定义和作用
语法:struct 结构体名 { 结构体成员列表 };
通过结构体创建变量的方式有三种:
- struct 结构体名变量名
- struct 结构体名 = {成员1值,成员2值…}
- 定义结构体时顺便创建变量
示例:
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 37
| #include<iostream> #include<string> using namespace std;
struct Student {
string name; int age; int score;
} s3;
int main() {
Student s1; s1.age = 20; s1.name = "张三"; s1.score = 100; cout << "姓名:" << s1.name << "\t年龄:" << s1.age << "\t分数:" << s1.score << endl; struct Student s2 = { "李四",21,101 }; cout << "姓名:" << s2.name << "\t年龄:" << s2.age << "\t分数:" << s2.score << endl; s3.age = 22; s3.name = "王五"; s3.score = 102; cout << "姓名:" << s3.name << "\t年龄:" << s3.age << "\t分数:" << s3.score << endl; system("pause"); return 0; }
|
- 总结1:定义结构体时的关键字是struct,不可省略
- 总结2:创建结构体变量时,关键字struct可以省略
- 总结3:结构体变量利用操作符"."访问成员
3. 结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名[元素个数]={ {} , {} , {} , ... , {}}
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include<iostream> #include<string> using namespace std;
struct Student {
string name; int age; int score;
};
int main() {
struct Student stus[3] = { {"张三",20,100},{"李四",21,101},{"王五",22,102} }; for (int i = 0; i < 3; i++) cout << "名字:" << stus[i].name << "\t年龄:" << stus[i].age << "\t分数:" << stus[i].score << endl;
system("pause"); return 0; }
|
4. 结构体指针
作用:通过指针访问结构体中的成员
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| struct Student {
string name; int age; int score;
};
int main() {
struct Student s = { "张三",18,100 };
struct Student * p = &s;
cout << "名字:" << p->name << "\t年龄:" << p->age << "\t分数:" << p->score << endl;
system("pause"); return 0; }
|
5. 结构体嵌套结构体
作用:结构体中的成员可以是另一结构体
例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
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
| 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 = 1000; t.name = "老王"; t.age = 50; t.stu.name = "小王"; t.stu.age = 20; t.stu.score = 60;
system("pause"); return 0; }
|
6. 结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式有两种:
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 37 38 39 40
| struct Student { string name; int age; int score;
};
void printStudent1(struct Student s) { s.age = 100; cout << "值传递函数打印 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl; }
void printStudent2(struct Student * p) { p->age = 200; cout << "地址传递函数打印 姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl; }
int main() { struct Student s; s.name = "张三"; s.age = 20; s.score = 85; printStudent2(&s);
cout << "main函数打印 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;
system("pause"); return 0; }
|
7. 结构体中const使用场景
作用:用const来防止误操作
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
|
struct Student { string name; int age; int score;
};
void printStudent(const struct Student * s) { cout << "值传递函数打印 姓名:" << s->name << " 年龄:" << s->age << " 分数:" << s->score << endl; }
int main() {
struct Student s; s.name = "张三"; s.age = 20; s.score = 85; printStudent(&s);
cout << "main函数打印 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;
system("pause"); return 0; }
|