1. 结构体的定义
结构体的定义使用 struct 关键字,其基本语法如下:
struct StructName {// 成员变量dataType1 member1;dataType2 member2;// ...
};
例如,定义一个表示学生信息的结构体:
[struct Student {]()
[ std::string name;]()
[ int age;]()
[ float score;]()
[};]()
2. 结构体变量的声明与初始化
2.1 声明结构体变量
可以在结构体定义之后声明结构体变量,也可以在定义结构体的同时声明变量。
// 在定义之后声明
Student s1;// 在定义的同时声明
struct Student {std::string name;int age;float score;
} s2;
2.2 初始化结构体变量
逐个成员初始化:
Student s3;
s3.name = "Alice";
s3.age = 20;
s3.score = 90.5;
使用初始化列表初始化:
Student s4 = {"Bob", 21, 85.0};
C++11 及以后版本支持列表初始化:
Student s5{"Charlie", 22, 78.5};
3. 访问结构体成员
使用点运算符(.)来访问结构体变量的成员。
std::cout << s4.name << std::endl;
std::cout << s4.age << std::endl;
std::cout << s4.score << std::endl;
4. 结构体作为函数参数
4.1 值传递
将结构体变量作为参数传递给函数时,会进行值拷贝
void printStudent(Student s) {std::cout << s.name << " " << s.age << " " << s.score << std::endl;
}int main() {Student s = {"David", 23, 92.0};printStudent(s);return 0;
}
4.2 引用传递
为了避免值拷贝带来的性能开销,可以使用引用传递。
void modifyStudent(Student& s) {s.score = 95.0;
}int main() {Student s = {"Eve", 24, 90.0};modifyStudent(s);std::cout << s.score << std::endl;return 0;
}
4.3 指针传递
也可以使用指针传递结构体变量。
void printStudentPtr(Student* s) {std::cout << s->name << " " << s->age << " " << s->score << std::endl;
}int main() {Student s = {"Frank", 25, 88.0};printStudentPtr(&s);return 0;
}
5. 结构体嵌套
结构体可以嵌套,即一个结构体的成员可以是另一个结构体。
struct Date {int year;int month;int day;
};struct Person {std::string name;Date birthday;
};int main() {Person p = {"Grace", {1990, 5, 15}};std::cout << p.name << " was born on " << p.birthday.year << "-" << p.birthday.month << "-" << p.birthday.day << std::endl;return 0;
}
6. 结构体数组
可以定义结构体数组,用于存储多个结构体对象。
Student students[3] = {{"Henry", 26, 87.0},{"Ivy", 27, 91.0},{"Jack", 28, 84.0}
};for (int i = 0; i < 3; ++i) {std::cout << students[i].name << std::endl;
}
步骤说明
定义结构体:使用 struct 关键字定义一个新的结构体类型,在结构体内部声明所需的成员变量。
使用结构体定义数组:就像定义普通数组一样,使用定义好的结构体类型来声明数组,同时可以选择对数组元素进行初始化。
示例代码
#include <iostream>
#include <string>// 步骤 1: 定义结构体
struct Book {std::string title;std::string author;int year;
};int main() {// 步骤 2: 使用结构体定义数组// 定义一个包含 3 个 Book 结构体元素的数组,并进行初始化Book library[3] = {{"The Great Gatsby", "F. Scott Fitzgerald", 1925},{"To Kill a Mockingbird", "Harper Lee", 1960},{"1984", "George Orwell", 1949}};// 遍历数组并输出每本书的信息for (int i = 0; i < 3; ++i) {std::cout << "Book " << i + 1 << ":\n";std::cout << "Title: " << library[i].title << "\n";std::cout << "Author: " << library[i].author << "\n";std::cout << "Year: " << library[i].year << "\n\n";}return 0;
}
其他注意事项
未初始化的数组:如果不进行初始化,数组元素的成员变量将具有未定义的值。例如:
Book library[3];
这里定义了一个包含 3 个 Book 结构体元素的数组,但没有对元素进行初始化,此时 library 数组中每个元素的 title、author 和 year 的值是不确定的。
动态数组:除了使用固定大小的数组,你还可以使用动态数组(如 std::vector)来存储结构体元素,这样可以在运行时动态调整数组的大小。示例如下:
#include <iostream>
#include <string>
#include <vector>struct Book {std::string title;std::string author;int year;
};int main() {std::vector<Book> library = {{"The Great Gatsby", "F. Scott Fitzgerald", 1925},{"To Kill a Mockingbird", "Harper Lee", 1960},{"1984", "George Orwell", 1949}};for (size_t i = 0; i < library.size(); ++i) {std::cout << "Book " << i + 1 << ":\n";std::cout << "Title: " << library[i].title << "\n";std::cout << "Author: " << library[i].author << "\n";std::cout << "Year: " << library[i].year << "\n\n";}return 0;
}
在这个示例中,使用 std::vector
7. 结构体中的成员函数
在 C++ 中,结构体可以包含成员函数,其用法和类的成员函数类似。
struct Rectangle {int width;int height;// 成员函数,计算矩形面积int area() {return width * height;}
};int main() {Rectangle r = {5, 10};std::cout << "Area: " << r.area() << std::endl;return 0;
}
8. 结构体和类的区别
在 C++ 中,结构体和类非常相似,主要区别在于默认的访问控制权限:
结构体的成员默认是公有的(public)。
类的成员默认是私有的(private)。
例如:
struct S {int a; // 默认 public
};class C {int b; // 默认 private
};
结构体在 C++ 中是一种非常灵活和实用的数据类型,可用于组织和管理相关的数据。通过合理使用结构体,可以使代码更加清晰和易于维护。