#include #include #include #include struct Student { int id; std::string name; std::string gender; int age; std::string major; }; class StudentManagementSystem { public: // 建立学生信息 std::vector students; void addStudent(const Student& s) { students.push_back(s); } // 查询学生信息 void searchStudent(int id) { bool found = false; for (const Student& s : students) { if (s.id == id) { std::cout << "学号:" << s.id << ", 姓名:" << s.name << ", 性别:" << s.gender << ", 年龄:" << s.age << ", 专业:" << s.major << std::endl; found = true; break; } } if (!found) { std::cout << "未找到该学号的学生。" << std::endl; } } // 删除学生信息 void deleteStudent(int id) { auto it = students.begin(); while (it != students.end()) { if (it->id == id) { it = students.erase(it); } else { ++it; } } } // 增加学生信息 void insertStudent(const Student& s) { students.push_back(s); } // 按学号排序 void sortByID() { std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) { return a.id < b.id; }); } }; int main() { StudentManagementSystem sms; // 添加一些学生信息 Student s1 = { 1, "张三", "男", 20, "计算机科学" }; Student s2 = { 2, "李四", "女", 21, "电子工程" }; Student s3 = { 3, "王五", "男", 19, "数学" }; sms.addStudent(s1); sms.addStudent(s2); sms.addStudent(s3); // 查询学生信息 sms.searchStudent(2); // 删除学生信息 sms.deleteStudent(2); // 增加学生信息 Student s4 = { 4, "赵六", "女", 22, "物理学" }; sms.insertStudent(s4); // 排序 sms.sortByID(); // 输出所有学生信息 std::cout << "所有学生信息:" << std::endl; for (const Student& s : sms.students) { std::cout << "学号:" << s.id << ", 姓名:" << s.name << ", 性别:" << s.gender << ", 年龄:" << s.age << ", 专业:" << s.major << std::endl; } return 0; }