简单数据结构
结构体
结构体的概念
在生活中,我们描述一个事物时,往往需要用到多个不同类型的信息。比如描述一个学生,可能需要他的姓名、年龄和成绩。在编程里,我们可以用结构体把这些不同类型的信息组合在一起,形成一个新的“数据类型”。
结构体的合法定义
定义结构体就像是设计一个新的“盒子”,这个“盒子”里可以装不同类型的东西。

基本语法
struct 结构体名称 {
数据类型 成员变量1;
数据类型 成员变量2;
// 可以有更多的成员变量
};
合法定义示例
// 定义一个名为Student的结构体,用来描述学生信息
struct Student {
// 成员变量name,用来存储学生的姓名,类型是字符数组
char name[20];
// 成员变量age,用来存储学生的年龄,类型是整数
int age;
// 成员变量score,用来存储学生的成绩,类型是小数
double score;
};
如何使用结构体
定义好结构体后,我们就可以用它来创建具体的“对象”,就像用设计好的“盒子”来装东西一样。
创建结构体变量
#include <iostream>
using namespace std;
// 定义一个名为Student的结构体
struct Student {
char name[20];
int age;
double score;
};
int main() {
// 创建一个Student类型的变量,名字叫stu
Student stu;
return 0;
}
给结构体成员变量赋值
#include <iostream>
#include <cstring>
using namespace std;
// 定义一个名为Student的结构体
struct Student {
char name[20];
int age;
double score;
};
int main() {
// 创建一个Student类型的变量,名字叫stu
Student stu;
// 给stu的name成员变量赋值为"小明"
strcpy(stu.name, "小明");
// 给stu的age成员变量赋值为10
stu.age = 10;
// 给stu的score成员变量赋值为90.5
stu.score = 90.5;
return 0;
}
访问结构体成员变量
#include <iostream>
#include <cstring>
using namespace std;
// 定义一个名为Student的结构体
struct Student {
char name[20];
int age;
double score;
};
int main() {
// 创建一个Student类型的变量,名字叫stu
Student stu;
// 给stu的name成员变量赋值为"小明"
strcpy(stu.name, "小明");
// 给stu的age成员变量赋值为10
stu.age = 10;
// 给stu的score成员变量赋值为90.5
stu.score = 90.5;
// 输出stu的name成员变量
cout << "姓名: " << stu.name << endl;
// 输出stu的age成员变量
cout << "年龄: " << stu.age << endl;
// 输出stu的score成员变量
cout << "成绩: " << stu.score << endl;
return 0;
}
例题
例题1:结构体数组
#include <iostream>
#include <cstring>
using namespace std;
// 定义一个名为Student的结构体
struct Student {
char name[20];
int age;
double score;
};
int main() {
// 创建一个能装3个Student类型变量的数组
Student students[3];
// 给第一个学生的信息赋值
strcpy(students[0].name, "小明");
students[0].age = 10;
students[0].score = 90.5;
// 给第二个学生的信息赋值
strcpy(students[1].name, "小红");
students[1].age = 11;
students[1].score = 92.0;
// 给第三个学生的信息赋值
strcpy(students[2].name, "小刚");
students[2].age = 10;
students[2].score = 88.5;
// 输出每个学生的信息
for (int i = 0; i < 3; i++) {
cout << "第" << i + 1 << "个学生信息:" << endl;
cout << "姓名: " << students[i].name << endl;
cout << "年龄: " << students[i].age << endl;
cout << "成绩: " << students[i].score << endl;
cout << endl;
}
return 0;
}
例题2:找出成绩最高的学生
#include <iostream>
#include <cstring>
using namespace std;
// 定义一个名为Student的结构体
struct Student {
char name[20];
int age;
double score;
};
int main() {
// 创建一个能装3个Student类型变量的数组
Student students[3];
// 给第一个学生的信息赋值
strcpy(students[0].name, "小明");
students[0].age = 10;
students[0].score = 90.5;
// 给第二个学生的信息赋值
strcpy(students[1].name, "小红");
students[1].age = 11;
students[1].score = 92.0;
// 给第三个学生的信息赋值
strcpy(students[2].name, "小刚");
students[2].age = 10;
students[2].score = 88.5;
// 假设第一个学生的成绩最高
int maxIndex = 0;
// 遍历数组,找出成绩最高的学生的索引
for (int i = 1; i < 3; i++) {
if (students[i].score > students[maxIndex].score) {
maxIndex = i;
}
}
// 输出成绩最高的学生的信息
cout << "成绩最高的学生信息:" << endl;
cout << "姓名: " << students[maxIndex].name << endl;
cout << "年龄: " << students[maxIndex].age << endl;
cout << "成绩: " << students[maxIndex].score << endl;
return 0;
}
通过上面的讲解和例题,你应该对结构体的定义和使用有了基本的了解。结构体可以帮助我们把相关的信息组织在一起,让程序更加清晰和方便。
例题
Status
Problem
Tags