在C++中,結構體數組的定義和使用可以通過以下步驟來實現:
struct Student {
string name;
int age;
float score;
};
Student students[3];
students[0].name = "Alice";
students[0].age = 20;
students[0].score = 85.5;
students[1].name = "Bob";
students[1].age = 21;
students[1].score = 90.0;
students[2].name = "Charlie";
students[2].age = 19;
students[2].score = 78.5;
for (int i = 0; i < 3; i++) {
cout << "Name: " << students[i].name << endl;
cout << "Age: " << students[i].age << endl;
cout << "Score: " << students[i].score << endl;
}
使用這些步驟,你就可以定義和使用結構體數組。注意,在使用結構體數組之前,需要包含相應的頭文件(例如,iostream)和使用命名空間(例如,using namespace std)。