C++ 结构体(struct)
在 C++ 中,结构体(struct) 用于定义一个数据类型,该数据类型可以包含多个不同类型的成员变量。结构体主要用于组织相关数据,使代码更加清晰和模块化。
1. 定义和使用结构体
在 C++ 中,struct 关键字用于定义结构体。结构体成员默认是 公有的(public),而类(class)默认是 私有的(private)。
1.1 结构体基本定义
#include <iostream>
// 定义结构体
struct Person {
std::string name;
int age;
double height;
};
int main() {
// 创建结构体变量并初始化
Person p1 = {"张三", 25, 1.75};
// 访问结构体成员
std::cout << "姓名: " << p1.name << std::endl;
std::cout << "年龄: " << p1.age << std::endl;
std::cout << "身高: " << p1.height << "m" << std::endl;
return 0;
}
输出:
姓名: 张三
年龄: 25
身高: 1.75m
2. 结构体数组
结构体可以用作数组的元素,以便存储多个相同类型的数据。
#include <iostream>
struct Student {
std::string name;
int age;
};
int main() {
// 结构体数组
Student students[3] = {
{"李四", 20},
{"王五", 21},
{"赵六", 22}
};
// 遍历结构体数组
for (int i = 0; i < 3; i++) {
std::cout << "姓名: " << students[i].name << ", 年龄: " << students[i].age << std::endl;
}
return 0;
}
输出:
姓名: 李四, 年龄: 20
姓名: 王五, 年龄: 21
姓名: 赵六, 年龄: 22
3. 结构体指针
使用 指针 访问结构体成员时,需要使用 ->(箭头运算符)。
#include <iostream>
struct Car {
std::string brand;
int year;
};
int main() {
Car myCar = {"Tesla", 2023};
Car *pCar = &myCar; // 指针指向结构体
// 通过指针访问结构体成员
std::cout << "品牌: " << pCar->brand << std::endl;
std::cout << "年份: " << pCar->year << std::endl;
return 0;
}
输出:
品牌: Tesla
年份: 2023
4. 结构体嵌套
结构体可以嵌套另一个结构体,从而创建更复杂的数据结构。
#include <iostream>
struct Address {
std::string city;
std::string street;
};
struct Employee {
std::string name;
int age;
Address address; // 嵌套结构体
};
int main() {
Employee emp = {"张三", 30, {"北京", "长安街"}};
std::cout << "姓名: " << emp.name << std::endl;
std::cout << "年龄: " << emp.age << std::endl;
std::cout << "城市: " << emp.address.city << std::endl;
std::cout << "街道: " << emp.address.street << std::endl;
return 0;
}
输出:
姓名: 张三
年龄: 30
城市: 北京
街道: 长安街
5. 结构体作为函数参数
结构体可以作为函数的 参数 和 返回值。
5.1 结构体作为参数(值传递)
#include <iostream>
struct Point {
int x, y;
};
// 传递结构体
void printPoint(Point p) {
std::cout << "坐标: (" << p.x << ", " << p.y << ")" << std::endl;
}
int main() {
Point p1 = {3, 4};
printPoint(p1);
return 0;
}
输出:
坐标: (3, 4)
注意: 结构体是值传递,函数内部修改
p不会影响main()中的p1。
5.2 结构体作为引用参数
为了提高性能,可以使用 引用 传递结构体,避免拷贝。
#include <iostream>
struct Point {
int x, y;
};
// 使用引用传递
void movePoint(Point &p, int dx, int dy) {
p.x += dx;
p.y += dy;
}
int main() {
Point p1 = {3, 4};
movePoint(p1, 2, 3);
std::cout << "移动后坐标: (" << p1.x << ", " << p1.y << ")" << std::endl;
return 0;
}
输出:
移动后坐标: (5, 7)
注意: 这里
p是引用传递,函数内部修改p会影响main()中的p1。
5.3 结构体作为返回值
#include <iostream>
struct Rectangle {
int width, height;
};
// 返回结构体
Rectangle createRectangle(int w, int h) {
return {w, h};
}
int main() {
Rectangle r = createRectangle(10, 5);
std::cout << "矩形宽: " << r.width << ", 高: " << r.height << std::endl;
return 0;
}
输出:
矩形宽: 10, 高: 5
6. 结构体与 typedef 和 using
在 C++ 中,可以使用 typedef 或 using 为结构体创建别名。
#include <iostream>
// 使用 typedef
typedef struct {
int x, y;
} Point;
// 使用 using
using Rectangle = struct {
int width, height;
};
int main() {
Point p = {2, 3};
Rectangle r = {5, 10};
std::cout << "Point: (" << p.x << ", " << p.y << ")" << std::endl;
std::cout << "Rectangle: " << r.width << " x " << r.height << std::endl;
return 0;
}
输出:
Point: (2, 3)
Rectangle: 5 x 10
7. 结构体和 class 的区别
| 特性 | struct(结构体) | class(类) |
|---|---|---|
| 默认访问权限 | public | private |
| 支持成员函数 | 是 | 是 |
| 适用于 | 纯数据存储 | 封装数据和方法 |
| 继承时默认权限 | public | private |
示例:
struct MyStruct {
int a;
}; // 默认 `public`
class MyClass {
int a; // 默认 `private`
};
总结
- 结构体 是存储多个数据项的方式,可以包含基本数据类型、数组、指针、函数等。
- 结构体可以作为数组、指针、函数参数和返回值。
- 可以使用引用传递结构体,避免拷贝,提高性能。
- 结构体和
class的主要区别是访问控制,结构体适用于纯数据存储,而类适用于面向对象编程。
更多详细内容请关注其他相关文章!