1.结构体的基本应用
结构体struct是一种用户自定义的复合数据类型,可以包含不同类型的成员。例如:
struct Studet
{
string name;
int age;
string gender;
}
结构体的声明定义和使用的基本语法:
struct 结构体类型
{
成员1类型 成员1名称;
···
成员n类型 成员n名称;
};
结构体定义和访问成员练习:
#include <iostream>
using namespace std;
int main() {
struct Student // 自己创建的新数据类型
{
string name; // 成员1 表示姓名
int age; // 成员2 表示年龄
string gender; // 成员3 表示性别
};
// 使用该类型创建变量
struct Student stu; // 结构体变量的声明可以省略struct关键字,建议写上这样便于区分自定义结构体与其他类型
// 结构体赋值
stu = {"周杰", 11, "man"};
// 输出结构体
// 错误写法 cout << stu; 因为结构体变量是一个整体的包装,无法直接cout输出
// 需要访问每一个成员进行输出
cout << stu.name << endl;
cout << stu.age << endl;
cout << stu.gender << endl;
// 声明和赋值同步写法
struct Student stu2 = {"lin", 20, "woman"};
cout << stu2.name << endl;
cout << stu2.age << endl;
cout << stu2.gender << endl;
return 0;
}
2.结构体成员的默认值
设计结构体时可以给成员设置一个默认值,例如:
struct Student {
string name;
string major_code = "083032"; // 默认专业代码
int dormitory_num = 1; // 默认分配1号宿舍
};
struct Student s1 = {"周末轮"};
// 不想使用默认值可以自己赋值
struct Student s2 = {"林军杰", "083082", 3};
代码演示本节内容:
#include <iostream>
using namespace std;
int main() {
struct Student {
string name;
string major_code = "083032"; // 默认专业代码
int dormitory_num = 1; // 默认分配1号宿舍
};
struct Student s1 = {"周末轮"};
// 不想使用默认值可以自己赋值
struct Student s2 = {"林军杰", "083082", 3};
cout << "学生1姓名" << s1.name << endl;
cout << "学生2姓名" << s2.name << endl;
cout << "学生2专业代码" << s2.major_code << endl;
cout << "学生2宿舍号" << s2.dormitory_num << endl;
return 0;
}
3.结构体数组
结构体支持数组模式。结构体数组的语法和普通的数组基本一样,只不过需要把结构体类型代入进去:
// 声明数组对象
[struct] 结构体类型 数组名[数组长度];
// 赋予数组的每一个元素
数组名[0]={,,,,,};
数组名[1]={,,,,,};
···
// 声明和赋值同步的快捷写法
[struct] 结构体类型 数组名 [数组长度] = {{},{},···,{}};
本节内容的代码演示:
#include <iostream>
using namespace std;
int main() {
struct Student {
string name;
string major_code = "083032"; // 默认专业代码
int dormitory_num = 1; // 默认分配1号宿舍
};
// 创建结构体数组
Student arr[3]; //结构体数组对象声明
arr[0] = {"张三", "083032", 1};
arr[1] = {"李四", "083032", 2};
arr[2] = {"王五", "083032", 3};
for (int i = 0; i < 3; i++) {
cout << "当前下标:" << i << "姓名是:" << arr[i].name << endl; // 通过.访问成员
cout << "当前下标:" << i << "专业代码是:" << arr[i].major_code << endl;
cout << "当前下标:" << i << "宿舍号是:" << arr[i].dormitory_num << endl;
}
// 数组的声明和赋值同步写法
Student students[2] = {
{"张三", "083032", 1},
{"李四", "234567", 0}
};
for (int i = 0; i < 2; i++) {
cout << "结构体数组2的下标:" << i << "姓名是:" << students[i].name << endl;
cout << "结构体数组2的下标:" << i << "专业代码是:" << students[i].major_code << endl;
cout << "结构体数组2的下标:" << i << "宿舍号是:" << students[i].dormitory_num << endl;
}
return 0;
}
4.结构体指针
结构体不仅支持数组,同样支持指针。以下面的代码为例,结构体类型的指针p指向结构体对象的内存地址。
struct Student {
string name;
string major_code = "083032"; // 默认专业代码
int dormitory_num = 1; // 默认分配1号宿舍
};
struct Student stu = {"张三", "003321", 5};
struct Student *p = &stu;
由于指针指向的地址是C++管理的,也就是静态内存管理,所以指针无法被回收。如果想要回收空间,可以使用动态内存管理,通过new操作符申请一个指针/结构体空间:
struct Student *p = new Student{"张三", "446712", 7};
->操作符
使用指针变量访问结构体成员需要更换操作符为:->
cout << p->name << endl;
cout << p->major_code << endl;
cout << p->dormitory_num << endl;
代码演示本节知识点:
#include <iostream>
using namespace std;
int main() {
struct Student {
string name;
string major_code = "083032"; // 默认专业代码
int dormitory_num = 1; // 默认分配1号宿舍
};
// 先创建一个标准的结构体对象(静态内存管理
struct Student stu = {"jay", "443321", 2};
// 创建结构体的指针,指向结构体对象的地址
struct Student * p = &stu;
// 通过结构体指针访问结构体的成员
cout << "结构体中成员的name: " << p->name << endl;
cout << "结构体中成员的major_code: " << p->major_code << endl;
cout << "结构体中成员的dormitory_num: " << p->dormitory_num << endl;
// 这种写法的指针无法回收内存空间
// new操作符申请结构体指针
struct Student *p2 = new Student{"jay", "443321", 2};
cout << "结构体中成员的name: " << p2->name << endl;
cout << "结构体中成员的major_code: " << p2->major_code << endl;
cout << "结构体中成员的dormitory_num: " << p2->dormitory_num << endl;
// 释放
delete p2;
return 0;
}