2、结构体数组.cpp 823 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <iostream>
  2. #include <string>
  3. //结构体数组
  4. //作用:将自定义的结构体放入到数组中方便维护
  5. //语法:struct 结构体名 数组名[元素个数] = { {} , {} , ...{} }
  6. //1、定义结构体
  7. #if(0)
  8. struct Student
  9. {
  10. //姓名
  11. std::string name;
  12. //年龄
  13. int age;
  14. //分数
  15. int score;
  16. };
  17. int main()
  18. {
  19. //2、创建一个结构体数组
  20. struct Student stuArry[8] =
  21. {
  22. {"张三" , 18 , 100},
  23. {"李四" , 28 , 99},
  24. {"王五" , 38 , 66}
  25. };
  26. //3、给结构体数组中的元素赋值
  27. stuArry[2].name = "赵六";
  28. stuArry[2].age = 80;
  29. stuArry[2].score = 60;
  30. //4、遍历结构体数组
  31. for (int i = 0; i < 3; i++)
  32. {
  33. std::cout << "姓名:" << stuArry[i].name
  34. << "年龄:" << stuArry[i].age
  35. << "分数:" << stuArry[i].score
  36. << std::endl;
  37. }
  38. system("pause");
  39. return 0;
  40. }
  41. #endif