关注公众号,后台回复
找书+ C++Primer
获取C++相关电子书。
C/C++ 数组允许定义可存储相同类型数据项的变量,但是结构是 C++ 中另一种用户自定义的可用的数据类型,它允许您存储不同类型的数据项。
结构用于表示一条记录,假设您想要跟踪图书馆中书本的动态,您可能需要跟踪每本书的下列属性:
-
Title :标题 -
Author :作者 -
Subject :类目 -
Book ID :书的 ID
在 C++ 中,struct 语句用于定义结构体(structure)。
结构体是一种用户自定义的数据类型,用于将不同类型的数据组合在一起。与类(class)类似,结构体允许你定义成员变量和成员函数。
定义结构体
为了定义结构,您必须使用 struct 语句。struct 语句定义了一个包含多个成员的新的数据类型,struct 语句的格式如下:
structtype_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
type_name 是结构体类型的名称,member_type1 member_name1 是标准的变量定义,比如 int i; 或者 float f; 或者其他有效的变量定义。在结构定义的末尾,最后一个分号之前,您可以指定一个或多个结构变量,这是可选的。
#include<iostream>
#include<string>
// 定义结构体 Person
structPerson {
std::string name;
int age;
float height;
};
intmain(){
// 创建一个 Person 结构体变量
Person person1;
// 设置结构体成员的值
person1.name = "Alice";
person1.age = 30;
person1.height = 165.5;
// 输出结构体成员的值
std::cout << "Name: " << person1.name << std::endl;
std::cout << "Age: " << person1.age << std::endl;
std::cout << "Height: " << person1.height << " cm" << std::endl;
return0;
}
---
Name: Alice
Age: 30
Height: 165.5 cm
结构体数组
#include<iostream>
#include<string>
// 定义结构体 Person
structPerson {
std::string name;
int age;
float height;
};
intmain(){
// 创建一个包含3个 Person 结构体的数组
Person people[3];
// 设置每个结构体成员的值
people[0].name = "张三";
people[0].age = 25;
people[0].height = 175.0;
people[1].name = "李四";
people[1].age = 28;
people[1].height = 180.0;
people[2].name = "王五";
people[2].age = 22;
people[2].height = 170.0;
// 输出每个结构体成员的值
for (int i = 0; i < 3; ++i) {
std::cout << "Name: " << people[i].name << std::endl;
std::cout << "Age: " << people[i].age << std::endl;
std::cout << "Height: " << people[i].height << " cm" << std::endl;
std::cout << std::endl;
}
return0;
}
---
Name: 张三
Age: 25
Height: 175 cm
Name: 李四
Age: 28
Height: 180 cm
Name: 王五
Age: 22
Height: 170 cm
结构体指针
#include<iostream>
#include<string>
// 定义结构体 Person
structPerson {
std::string name;
int age;
float height;
};
intmain(){
// 创建一个包含3个 Person 结构体的数组
Person people[3];
// 设置每个结构体成员的值
people[0].name = "张三";
people[0].age = 25;
people[0].height = 175.0;
people[1].name = "李四";
people[1].age = 28;
people[1].height = 180.0;
people[2].name = "王五";
people[2].age = 22;
people[2].height = 170.0;
// 使用结构体指针访问和输出每个结构体成员的值
for (int i = 0; i < 3; ++i) {
// 定义指向当前结构体的指针
Person* ptr = &people[i]; // 获取当前结构体的地址
// 使用指针访问结构体成员
std::cout << "Name: " << ptr->name << std::endl; // 使用 -> 运算符访问结构体成员
std::cout << "Age: " << ptr->age << std::endl;
std::cout << "Height: " << ptr->height << " cm" << std::endl;
std::cout << std::endl;
}
return0;
}
---
Name: 张三
Age: 25
Height: 175 cm
Name: 李四
Age: 28
Height: 180 cm
Name: 王五
Age: 22
Height: 170 cm
结构体套结构体(套娃)
#include<iostream>
#include<string>
// 定义结构体 Address
structAddress {
std::string street;
std::string city;
int postalCode;
};
// 定义结构体 Person
structPerson {
std::string name;
int age;
float height;
Address address; // 嵌套 Address 结构体
};
intmain(){
// 创建一个包含3个 Person 结构体的数组
Person people[3];
// 设置每个结构体成员的值
people[0].name = "张三";
people[0].age = 25;
people[0].height = 175.0;
people[0].address.street = "123 Main St";
people[0].address.city = "Beijing";
people[0].address.postalCode = 100000;
people[1].name = "李四";
people[1].age = 28;
people[1].height = 180.0;
people[1].address.street = "456 Elm St";
people[1].address.city = "Shanghai";
people[1].address.postalCode = 200000;
people[2].name = "王五";
people[2].age = 22;
people[2].height = 170.0;
people[2].address.street = "789 Oak St";
people[2].address.city = "Guangzhou";
people[2].address.postalCode = 510000;
// 使用结构体指针访问和输出每个结构体成员的值
for (int i = 0; i < 3; ++i) {
// 定义指向当前结构体的指针
Person* ptr = &people[i];
// 使用指针访问结构体成员
std::cout << "Name: " << ptr->name << std::endl;
std::cout << "Age: " << ptr->age << std::endl;
std::cout << "Height: " << ptr->height << " cm" << std::endl;
std::cout << "Address: " << ptr->address.street << ", " << ptr->address.city << ", " << ptr->address.postalCode << std::endl;
std::cout << std::endl;
}
return0;
}
---
Name: 张三
Age: 25
Height: 175 cm
Address: 123 Main St, Beijing, 100000
Name: 李四
Age: 28
Height: 180 cm
Address: 456 Elm St, Shanghai, 200000
Name: 王五
Age: 22
Height: 170 cm
Address: 789 Oak St, Guangzhou, 510000
结构体做函数参数
#include<iostream>
#include<string>
// 定义结构体 Address
structAddress {
std::string street;
std::string city;
int postalCode;
};
// 定义结构体 Person
structPerson {
std::string name;
int age;
float height;
Address address; // 嵌套 Address 结构体
};
// 定义输出 Person 结构体成员值的函数,使用点运算符
voidprintPersonByDot(const Person& person){
std::cout << "Name: " << person.name << std::endl;
std::cout << "Age: " << person.age << std::endl;
std::cout << "Height: " << person.height << " cm" << std::endl;
std::cout << "Address: " << person.address.street << ", " << person.address.city << ", " << person.address.postalCode << std::endl;
std::cout << std::endl;
}
// 定义输出 Person 结构体成员值的函数,使用箭头运算符
voidprintPersonByArrow(const Person* person){
std::cout << "Name: " << person->name << std::endl;
std::cout << "Age: " << person->age << std::endl;
std::cout << "Height: " << person->height << " cm" << std::endl;
std::cout << "Address: " << person->address.street << ", " << person->address.city << ", " << person->address.postalCode << std::endl;
std::cout << std::endl;
}
intmain(){
// 创建一个包含3个 Person 结构体的数组
Person people[3];
// 设置每个结构体成员的值
people[0].name = "张三";
people[0].age = 25;
people[0].height = 175.0;
people[0].address.street = "123 Main St";
people[0].address.city = "Beijing";
people[0].address.postalCode = 100000;
people[1].name = "李四";
people[1].age = 28;
people[1].height = 180.0;
people[1].address.street = "456 Elm St";
people[1].address.city = "Shanghai";
people[1].address.postalCode = 200000;
people[2].name = "王五";
people[2].age = 22;
people[2].height = 170.0;
people[2].address.street = "789 Oak St";
people[2].address.city = "Guangzhou";
people[2].address.postalCode = 510000;
// 使用 printPersonByDot 函数输出每个结构体成员的值
for (int i = 0; i < 3; ++i) {
printPersonByDot(people[i]);
}
// 使用 printPersonByArrow 函数输出每个结构体成员的值
for (int i = 0; i < 3; ++i) {
printPersonByArrow(&people[i]);
}
return0;
}
---
Name: 李四
Age: 28
Height: 180 cm
Address: 456 Elm St, Shanghai, 200000
Name: 王五
Age: 22
Height: 170 cm
Address: 789 Oak St, Guangzhou, 510000
Name: 张三
Age: 25
Height: 175 cm
Address: 123 Main St, Beijing, 100000
Name: 李四
Age: 28
Height: 180 cm
Address: 456 Elm St, Shanghai, 200000
Name: 王五
Age: 22
Height: 170 cm
Address: 789 Oak St, Guangzhou, 510000
const的作用
不使用 const 可能导致的结构体成员被意外修改。
#include<iostream>
#include<string>
// 定义结构体 Address
structAddress {
std::string street;
std::string city;
int postalCode;
};
// 定义结构体 Person
structPerson {
std::string name;
int age;
float height;
Address address; // 嵌套 Address 结构体
};
// 定义输出 Person 结构体成员值的函数,使用点运算符
voidprintPersonByDot(Person& person){
// 修改结构体成员的值
person.name = "Modified";
person.age = 99;
person.height = 999.9;
person.address.street = "Modified St";
person.address.city = "Modified City";
person.address.postalCode = 999999;
std::cout << "Name: " << person.name << std::endl;
std::cout << "Age: " << person.age << std::endl;
std::cout << "Height: " << person.height << " cm" << std::endl;
std::cout << "Address: " << person.address.street << ", " << person.address.city << ", " << person.address.postalCode << std::endl;
std::cout << std::endl;
}
// 定义输出 Person 结构体成员值的函数,使用箭头运算符
voidprintPersonByArrow(Person* person){
// 修改结构体成员的值
person->name = "Modified";
person->age = 99;
person->height = 999.9;
person->address.street = "Modified St";
person->address.city = "Modified City";
person->address.postalCode = 999999;
std::cout << "Name: " << person->name << std::endl;
std::cout << "Age: " << person->age << std::endl;
std::cout << "Height: " << person->height << " cm" << std::endl;
std::cout << "Address: " << person->address.street << ", " << person->address.city << ", " << person->address.postalCode << std::endl;
std::cout << std::endl;
}
intmain(){
// 创建一个包含3个 Person 结构体的数组
Person people[3];
// 设置每个结构体成员的值
people[0].name = "张三";
people[0].age = 25;
people[0].height = 175.0;
people[0].address.street = "123 Main St";
people[0].address.city = "Beijing";
people[0].address.postalCode = 100000;
people[1].name = "李四";
people[1].age = 28;
people[1].height = 180.0;
people[1].address.street = "456 Elm St";
people[1].address.city = "Shanghai";
people[1].address.postalCode = 200000;
people[2].name = "王五";
people[2].age = 22;
people[2].height = 170.0;
people[2].address.street = "789 Oak St";
people[2].address.city = "Guangzhou";
people[2].address.postalCode = 510000;
// 输出修改前的结构体成员值
std::cout << "Before modification:" << std::endl;
for (int i = 0; i < 3; ++i) {
std::cout << "Name: " << people[i].name << std::endl;
std::cout << "Age: " << people[i].age << std::endl;
std::cout << "Height: " << people[i].height << " cm" << std::endl;
std::cout << "Address: " << people[i].address.street << ", " << people[i].address.city << ", " << people[i].address.postalCode << std::endl;
std::cout << std::endl;
}
// 使用 printPersonByDot 函数输出并修改每个结构体成员的值
std::cout << "Using printPersonByDot (modifies the structure):" << std::endl;
for (int i = 0; i < 3; ++i) {
printPersonByDot(people[i]);
}
// 输出修改后的结构体成员值
std::cout << "After printPersonByDot modification:" << std::endl;
for (int i = 0; i < 3; ++i) {
std::cout << "Name: " << people[i].name << std::endl;
std::cout << "Age: " << people[i].age << std::endl;
std::cout << "Height: " << people[i].height << " cm" << std::endl;
std::cout << "Address: " << people[i].address.street << ", " << people[i].address.city << ", " << people[i].address.postalCode << std::endl;
std::cout << std::endl;
}
// 使用 printPersonByArrow 函数输出并修改每个结构体成员的值
std::cout << "Using printPersonByArrow (modifies the structure):" << std::endl;
for (int i = 0; i < 3; ++i) {
printPersonByArrow(&people[i]);
}
// 输出修改后的结构体成员值
std::cout << "After printPersonByArrow modification:" << std::endl;
for (int i = 0; i < 3; ++i) {
std::cout << "Name: " << people[i].name << std::endl;
std::cout << "Age: " << people[i].age << std::endl;
std::cout << "Height: " << people[i].height << " cm" << std::endl;
std::cout << "Address: " << people[i].address.street << ", " << people[i].address.city << ", " << people[i].address.postalCode << std::endl;
std::cout << std::endl;
}
return0;
}
---
Before modification:
Name: 张三
Age: 25
Height: 175 cm
Address: 123 Main St, Beijing, 100000
Name: 李四
Age: 28
Height: 180 cm
Address: 456 Elm St, Shanghai, 200000
Name: 王五
Age: 22
Height: 170 cm
Address: 789 Oak St, Guangzhou, 510000
Using printPersonByDot(modifies the structure):
Name: Modified
Age: 99
Height: 999.9 cm
Address: Modified St, Modified City, 999999
Name: Modified
Age: 99
Height: 999.9 cm
Address: Modified St, Modified City, 999999
Name: Modified
Age: 99
Height: 999.9 cm
Address: Modified St, Modified City, 999999
After printPersonByDot modification:
Name: Modified
Age: 99
Height: 999.9 cm
Address: Modified St, Modified City, 999999
Name: Modified
Age: 99
Height: 999.9 cm
Address: Modified St, Modified City, 999999
Name: Modified
Age: 99
Height: 999.9 cm
Address: Modified St, Modified City, 999999
Using printPersonByArrow(modifies the structure):
Name: Modified
Age: 99
Height: 999.9 cm
Address: Modified St, Modified City, 999999
Name: Modified
Age: 99
Height: 999.9 cm
Address: Modified St, Modified City, 999999
Name: Modified
Age: 99
Height: 999.9 cm
Address: Modified St, Modified City, 999999
After printPersonByArrowmodification:
Name: Modified
Age: 99
Height: 999.9 cm
Address: Modified St, Modified City, 999999
Name: Modified
Age: 99
Height: 999.9 cm
Address: Modified St, Modified City, 999999
Name: Modified
Age: 99
Height: 999.9 cm
Address: Modified St, Modified City, 999999
结构体案例
1-老师带学生
学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下:
-
设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员 -
学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值 -
最终打印出老师数据以及老师所带的学生教据,
#include<iostream>
#include<string>
// 定义结构体 Student
structStudent {
std::string name;
int score;
};
// 定义结构体 Teacher
structTeacher {
std::string name;
Student students[5]; // 嵌套存放5名学生的数组
};
// 给每个老师及其学生赋值的函数
voidassignValues(Teacher teachers[], int teacherCount){
for (int i = 0; i < teacherCount; ++i) {
teachers[i].name = "Teacher" + std::to_string(i + 1);
for (int j = 0; j < 5; ++j) {
teachers[i].students[j].name = "Student" + std::to_string(i * 5 + j + 1);
teachers[i].students[j].score = 60 + (i * 5 + j) % 40; // 随机分数
}
}
}
// 打印老师及其学生数据的函数
voidprintTeachers(const Teacher teachers[], int teacherCount){
for (int i = 0; i < teacherCount; ++i) {
std::cout << "Teacher Name: " << teachers[i].name << std::endl;
for (int j = 0; j < 5; ++j) {
std::cout << " Student Name: " << teachers[i].students[j].name
<< ", Score: " << teachers[i].students[j].score << std::endl;
}
std::cout << std::endl;
}
}
intmain(){
// 创建一个包含3个 Teacher 结构体的数组
Teacher teachers[3];
// 给每个老师及其学生赋值
assignValues(teachers, 3);
// 打印老师及其学生数据
printTeachers(teachers, 3);
return0;
}
---
Teacher Name: Teacher1
Student Name: Student1, Score: 60
Student Name: Student2, Score: 61
Student Name: Student3, Score: 62
Student Name: Student4, Score: 63
Student Name: Student5, Score: 64
Teacher Name: Teacher2
Student Name: Student6, Score: 65
Student Name: Student7, Score: 66
Student Name: Student8, Score: 67
Student Name: Student9, Score: 68
Student Name: Student10, Score: 69
Teacher Name: Teacher3
Student Name: Student11, Score: 70
Student Name: Student12, Score: 71
Student Name: Student13, Score: 72
Student Name: Student14, Score: 73
Student Name: Student15, Score: 74
2-英雄排名
设计一个英雄的结构体,包括成员姓名,年龄,性别创建结构体数组,数组中存放5名英雄。 通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
#include<iostream>
#include<string>
// 定义结构体 Hero
structHero {
std::string name;
int age;
std::string gender;
};
// 冒泡排序函数,按年龄升序排序
voidbubbleSort(Hero heroes[], int count){
for (int i = 0; i < count - 1; ++i) {
for (int j = 0; j < count - 1 - i; ++j) {
if (heroes[j].age > heroes[j + 1].age) {
// 交换两个英雄的位置
Hero temp = heroes[j];
heroes[j] = heroes[j + 1];
heroes[j + 1] = temp;
}
}
}
}
// 打印英雄数组的函数
voidprintHeroes(const Hero heroes[], int count){
for (int i = 0; i < count; ++i) {
std::cout << "Name: " << heroes[i].name << ", Age: " << heroes[i].age << ", Gender: " << heroes[i].gender << std::endl;
}
}
intmain(){
// 创建一个包含5个 Hero 结构体的数组
Hero heroes[5] = {
{"刘备", 23, "男"},
{"关羽", 22, "男"},
{"张飞", 20, "男"},
{"赵云", 21, "男"},
{"貂蝉", 19, "女"}
};
// 打印排序前的英雄数组
std::cout << "Before sorting:" << std::endl;
printHeroes(heroes, 5);
// 对英雄数组进行冒泡排序
bubbleSort(heroes, 5);
// 打印排序后的英雄数组
std::cout << "After sorting:" << std::endl;
printHeroes(heroes, 5);
return0;
}
---
Before sorting:
Name: 刘备, Age: 23, Gender: 男
Name: 关羽, Age: 22, Gender: 男
Name: 张飞, Age: 20, Gender: 男
Name: 赵云, Age: 21, Gender: 男
Name: 貂蝉, Age: 19, Gender: 女
After sorting:
Name: 貂蝉, Age: 19, Gender: 女
Name: 张飞, Age: 20, Gender: 男
Name: 赵云, Age: 21, Gender: 男
Name: 关羽, Age: 22, Gender: 男
Name: 刘备, Age: 23, Gender: 男
🔔 想要获取更多网络安全与编程技术干货?
关注 泷羽Sec-静安 公众号,与你一起探索前沿技术,分享实用的学习资源与工具。我们专注于深入分析,拒绝浮躁,只做最实用的技术分享!💻
扫描下方二维码,马上加入我们,共同成长!🌟
👉 长按或扫描二维码关注公众号
或者直接回复文章中的关键词,获取更多技术资料与书单推荐!📚
原文始发于微信公众号(泷羽Sec-静安):08x结构体64-71
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论