C++学习笔记——结构体

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;

// 1、创建学生数据类型:学生包括(姓名,年龄,分数)
// 语法 struct 类型名称 {成员列表}
struct Student {

//成员列表
string name;
int age;
int score;

} s3;//顺便常见结构体变量

// 2、通过学生类型创建具体学生

int main() {

// 2.1 struct Student s1
//struct 关键字可以省略
Student s1;
s1.age = 20;
s1.name = "张三";
s1.score = 100;
cout << "姓名:" << s1.name << "\t年龄:" << s1.age << "\t分数:" << s1.score << endl;
// 2.2 struct Student s2 = {...}
struct Student s2 = { "李四",21,101 };
cout << "姓名:" << s2.name << "\t年龄:" << s2.age << "\t分数:" << s2.score << endl;
// 2.3 在定义结构体时顺便创建结构体变量 (不建议)
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() {

//1、创建结构体变量
struct Student s = { "张三",18,100 };

//2、通过指针指向结构体变量(struct可以省略)
struct Student * p = &s;

//3、通过指针访问结构体变量中的数据

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;

};


//打印学生信息的函数
//1、值传递 不会改变实参的值
void printStudent1(struct Student s) {
s.age = 100;
cout << "值传递函数打印 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;
}

//2、地址传递 会改变实参的值
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;

//printStudent1(s);
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
// const使用场景

// 定义学生结构体
struct Student {
string name;
int age;
int score;

};

// 将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
// 传入指针,只占4个字节,比值传递大幅节省空间
void printStudent(const struct Student * s) {
//s->age = 150; 加入const之后,只能读不能写,一旦有修改的操作就会报错,可以防止我们误操作
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;
}