• 语法:

    1. struct 结构体名 { 结构体成员列表 }
  • 通过结构体创建的方式有三种

    • struct 结构体名 变量名
    • struct 结构体名 变量名 = { 成员1值,成员2值 }
    • 定义结构体时顺便创建变量 ```cpp

      include

      include

using namespace std;

// 1.创建学生的数据类型 struct Student { // 成员列表 string name; int age; int score; } s3; // s3 在顺便闯将结构体变量

int main() { // 2.通过学生类型创建具体学生 // 2.1 struct Student s1 struct Student s1; // 给s1 属性赋值 s1.name = “test”; s1.age = 10; s1.score = 90;

  1. cout << "姓名" << s1.name << endl;
  2. cout << "年龄" << s1.age << endl;
  3. cout << "分数" << s1.score << endl;
  4. // 2.2 struct Student s2 = {...}
  5. struct Student s2 = {"admin", 20, 50};
  6. cout << "姓名" << s2.name << endl;
  7. cout << "年龄" << s2.age << endl;
  8. cout << "分数" << s2.score << endl;
  9. // 2.3 在定义结构体时顺便创建结构体变量
  10. s3.name = "admin1-1";
  11. s3.age = 30;
  12. s3.score = 100;
  13. cout << "姓名" << s3.name << endl;
  14. cout << "年龄" << s3.age << endl;
  15. cout << "分数" << s3.score << endl;
  16. return 0;

} ```