是否有一个ResultSet方法,我可以使用它来搜索ResultSet并检查它是否具有特定的值/元素?
与ArrayList.contains()方法类似.
如果没有,你不需要输入搜索方法,我会做一个:)
提前致谢.
程序从文件中逐行读取并在结构中存储信息.一切都有效,除了排序结构数组.例如,在我打印结构(最后包含的代码)的最后,它完全正常.
当我调用qsort时会出现问题(分段错误).
此外,打印学生[0] .lastName工作正常,但打印学生[1] .lastName返回一个(null),这也是令人困惑的.
我到处寻找,我的代码看起来非常类似于已经发布的排序结构的正确解决方案,所以我很困惑.
在main的头文件中定义struct:
// DEFINE STRUCT
typedef struct _StudentInformation {
int term;
int studentId;
char *lastName;
char *firstName;
char *subject;
int catalogNumber;
char *section;
} StudentInformation;
Run Code Online (Sandbox Code Playgroud)
在main方法中分配struct(STUDENT_DATA = 50):
// ALLOCATE AN ARRAY OF STUDENTS (STRUCT)
StudentInformation *students;
if ((students = malloc(STUDENT_DATA*sizeof(StudentInformation)))==NULL) {
scanf("Error can't allocate enough students!\n");
exit(1);
}
Run Code Online (Sandbox Code Playgroud)
问题:调用quicksort(8的原因是因为有8个条目工作并且被加载,甚至少于8个不起作用):
qsort(students, 8, sizeof(StudentInformation), comparator);
Run Code Online (Sandbox Code Playgroud)
快速排序比较器:
int comparator (const void * a, const void * b) {
StudentInformation *s1 = (StudentInformation*)a;
StudentInformation *s2 = (StudentInformation*)b;
return …Run Code Online (Sandbox Code Playgroud)