软件设计-Tutorial18
package Tutorial18; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; // 定义学生类 class Student { private String name; private String studentId; private int age; public Student(String name, String studentId, int age) { this.name = name; this.studentId = studentId; this.age = age; } public String getName() { return name; } public String getStudentId() { return studentId; } public int getAge() { return age; } } public class StudentInfoTraversal { public static void main(String[] args) { // 创建学生列表 List<Student> students = new ArrayList<>(); students.add(new Student("张三", "130501", 20)); students.add(new Student("李四", "130502", 21)); students.add(new Student("王五", "130503", 21)); students.add(new Student("赵六", "130504", 21)); students.add(new Student("mzd", "130505", 21)); students.add(new Student("xxk", "130506", 21)); students.add(new Student("qfy", "130507", 21)); students.add(new Student("gyg", "130508", 21)); students.add(new Student("czf", "130509", 21)); // 可以继续添加更多学生信息 // 按照学号从小到大排序并输出 Collections.sort(students, new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { return s1.getStudentId().compareTo(s2.getStudentId()); } }); System.out.println("按照学号从小到大输出学生信息:"); Iterator<Student> ascendingIterator = students.iterator(); while (ascendingIterator.hasNext()) { Student student = ascendingIterator.next(); System.out.println("姓名:" + student.getName() + ",学号:" + student.getStudentId() + ",年龄:" + student.getAge()); } // 按照学号从大到小排序并输出 Collections.sort(students, new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { return s2.getStudentId().compareTo(s1.getStudentId()); } }); System.out.println("按照学号从大到小输出学生信息:"); Iterator<Student> descendingIterator = students.iterator(); while (descendingIterator.hasNext()) { Student student = descendingIterator.next(); System.out.println("姓名:" + student.getName() + ",学号:" + student.getStudentId() + ",年龄:" + student.getAge()); } } }
Cpp: #include <iostream> #include <vector> #include <algorithm> #include <string> // 定义学生结构体 struct Student { std::string name; std::string studentId; int age; Student(const std::string& name, const std::string& studentId, int age) : name(name), studentId(studentId), age(age) {} }; // 比较函数,用于按照学号从小到大排序 bool compareAscending(const Student& s1, const Student& s2) { return s1.studentId < s2.studentId; } // 比较函数,用于按照学号从大到小排序 bool compareDescending(const Student& s1, const Student& s2) { return s1.studentId > s2.studentId; } int main() { // 创建学生向量 std::vector<Student> students; students.push_back(Student("张三", "130501", 20)); students.push_back(Student("李四", "130502", 21)); students.push_back(Student("王五", "130503", 21)); students.push_back(Student("赵六", "130504", 21)); students.push_back(Student("mzd", "130505", 21)); students.push_back(Student("xxk", "130506", 21)); students.push_back(Student("qfy", "130507", 21)); students.push_back(Student("gyg", "130508", 21)); students.push_back(Student("czf", "130509", 21)); // 可以继续添加更多学生信息 // 按照学号从小到大排序并输出 std::sort(students.begin(), students.end(), compareAscending); std::cout << "按照学号从小到大输出学生信息:" << std::endl; for (const auto& student : students) { std::cout << "姓名:" << student.name << ",学号:" << student.studentId << ",年龄:" << student.age << std::endl; } // 按照学号从大到小排序并输出 std::sort(students.begin(), students.end(), compareDescending); std::cout << "按照学号从大到小输出学生信息:" << std::endl; for (const auto& student : students) { std::cout << "姓名:" << student.name << ",学号:" << student.studentId << ",年龄:" << student.age << std::endl; } return 0; }