结构体的定义
在C++中,结构体(struct)是一种用户定义的数据类型,它允许你组合不同类型的变量作为其成员。结构体提供了一种方式来组织和操作多个不同类型的数据,使得它们作为一个整体来处理。
#include<iostream>
#include<string>
using namespace std;
struct student {//结构体的定义
//成员列表
string name;//姓名
int age;//年龄
int score;//分数
}stu3;//结构体创建方式3
int main() {
//结构体创建方式1
struct student stu1;//关键字可以省略
stu1.name = "张无忌";
stu1.age = 18;
stu1.score = 100;
cout << "姓名:" << stu1.name << "年龄:" << stu1.age << "分数:" << stu1.score << endl;
//结构体变量的创建方式2
struct student stu2 = { "李素裳",19,30 };
cout << "姓名:" << stu2.name << "年龄:" << stu2.age << "分数:" << stu2.score << endl;
system("pause");
return 0;
}
结构体的创建方式分为三种:
1.在main函数中定义,需要分别设定结构体的几种项目(struct关键字可以省略)
struct student stu1;//关键字可以省略
stu1.name = "张无忌";
stu1.age = 18;
stu1.score = 100;
2.自定义一个函数,在函数中设定结构体的项目,之后在main函数中使用,结构体的定义通常包括一个关键字 struct 后跟结构体名称以及一对花括号 {} 包围的成员列表。成员可以是不同的数据类型,如整型、浮点型、字符串等。
struct Person {
string name;
int age;
float height;
};
3.直接增添数组的形式,不过仍需先定义函数,再用数组赋值
struct student stu2 = { "李素裳",19,30 };
结构体中的数组
结构体中可以创建多维数组;
还可以更改数组中特定位置的内容;
#include<iostream>
using namespace std;
//结构体数组
//1.定义结构体
struct student {
//姓名
string name;
//年龄
int age;
//分数
int score;
};
int main() {
//2.创建结构体数组
struct student stuArray[3] {
{"张无忌",21,95 },
{"李素裳",19,50},
{"赵政",45,98}
};
//3.给结构体数组中的元素赋值
stuArray[2].name = "徐福";
stuArray[2].age =30 ;
stuArray[2].score = 70;
//4.遍历结构体数组
for (int i=0; i < 3; i++) {
cout << "姓名:" << stuArray[i].name
<< "年龄:" << stuArray[i].age
<< "分数:" << stuArray[i].score << endl;
}
system("pause");
return 0;
}
结构体中的指针
- 可以创建指向结构体的指针,并通过指针访问结构体的成员
Person *p = &person1;
cout << p->name << " is " << p->age << " years old." << endl;
-
指针的优点在于只传递地址,这样可以最大限度的节省内存;
-
需要注意的是指针的传递方式,Student* p = &stu;将数组传递给指针变量,以及,p->name则是指针的特殊调取内部项目的方式
#include<iostream>
using namespace std;
#include<String>
struct Student {
string name;
int age;
int score;
};
int main() {
struct Student stu = { "华成城",56,75 };
Student* p = &stu;
cout << "姓名:" << p->name
<< "年龄:" << p->age
<< "分数:" << p->score << endl;
system("pause");
}
结构体函数中的const使用方式
结构体可以作为函数的参数传递。
- 传递的方式有两种: 按值传递和按引用传递。
- 按值传递:传递的是结构体的一个副本,函数内的修改不会影响到原来的结构体。
- 按引用传递:传递的是结构体的引用,函数内的修改会影响到原来的结构体.
下面介绍函数的定义代码示例
void updateAge(Person &p) {
p.age += 1;
}
updateAge(person1); // person1.age 将会增加1
在函数中const将结构体输入锁定,防止误操作,因此在本函数内无法更改数据
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int age;
int score;
};
//每一次传递数据都会占用大量的内存
void PrintStudent(struct Student s) {
s.age = 750;
cout << "姓名:" << s.name << "年龄:" << s.age << "分数:" << s.score << endl;
}
//指针的优点就是只传递地址,这样可以最大限度的节省内存
void PrintStudent1(const Student* p) {
//p->age = 8000;//const将结构体输入锁定防止误操作,因此在本函数内无法更改数据
cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
}
int main() {
struct Student s;
s.name = "林传庭";
s.age = 25;
s.score = 80;
PrintStudent(s);
PrintStudent1(&s);
system("pause");
}
结构体的嵌套
在一个结构体体中嵌套另一个结构体可以访问和更改,赋值其子结构体的数据
#include<iostream>
using namespace std;
#include<String>
struct Student {
string name;
int age;
int score;
};
struct Teacher {
string name;
int id;
int age;
struct Student stu;
};
int main() {
Teacher t;
t.name = "罗世宗";
t.age = 45;
t.id = 945;
t.stu.name = "太史";
t.stu.age = 25;
t.stu.score = 35;
cout << "师承:" << t.name << "年龄:" << t.age << "教师编号:" << t.id
<< "姓名:" << t.stu.name
<< "年龄:" << t.stu.age
<< "分数:" << t.stu.score << endl;
system("pause");
}
结构体的内存布局
结构体在内存中是连续存储的,这意味着所有成员都紧密排列在一起。这对于访问速度是有利的,但也可能导致内存对齐问题。为了提高性能,有时候需要对结构体成员的顺序进行调整以优化内存对齐
小结
这些基本知识点涵盖了在C++中使用结构体时的一些重要概念和实践技巧。理解和熟练掌握这些内容有助于编写出高效且易于维护的代码。