- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
#include <stdio.h>
typedef struct
{ int occurrence
char charArray[101];
}Wordy;
int comparing(const void *a, const void *b)
{
//....
}
int main(void)
{
int i=0;
Wordy array[99999];
//.....
return 0;
}
*/
这个程序从文件中读取单词,然后计算单词的出现次数。一切正常,但我认为 qsort 函数在比较函数中无法正常工作。它假设根据它的出现对 array
进行排序。 (发现它的出现是正确的。)
我的示例文本文件:
zero one
two zero
three three
输出:
Before: zero one two three
After: one two three
(注意排序后少了0)
最佳答案
这个:
qsort(array, i, sizeof(int), comparing);
是错误的,array
不是int
的数组,而是Wordy
的数组。当您为 qsort()
指定错误的元素大小时,结果或多或少是随机的,因为您将获得指向 int
大小的“切片”的指针Worddy
结构数组。
尝试:
qsort(array, i, sizeof array[0], comparing);
关于c - 使用 qsort 对结构数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21141250/
我正在尝试对这个字符串列表进行排序:["a", "z", "b"]。所以答案应该是 ["a", "b", "z"]。但是,当我尝试使用 C 的 qsort() 时,没有任何 Action !我做错了什
我尝试使用 qsort 创建一些基本代码来对字符串数组进行排序,但根据 gdb,它在 qsort 中崩溃了: #include #include static int pcmp(const voi
我想使用 qsort(...) 对 2D int 数组进行排序,但我的比较函数似乎是错误的。 qsort的参数如下: qsort(void *base, size_t nitems, size_t s
我一直在寻找一种方法来对 C 中的数组子集进行排序,而无需将元素移动到临时数组并将它们复制回来。我可能对 qsort 理解不好,但我认为下面的代码应该可以工作: qsort(&my_struct_ar
我正在使用标准 c 库的 qsort 函数对组织在数组中的数百万个结构进行排序。我试图通过创建具有相同长度的结构指针数组来优化性能。与我的预期相反,第二个变体的执行时间较慢: qsort 结构数组:1
我成功地对结构数组进行了排序,其中每个结构仅包含一个 char 字符串。然而,我的问题是,对于大约的结构数组。 900,000 个元素,qsort 比我预期的要长很多(qsort 需要大约 2 分钟来
这个问题在这里已经有了答案: How to qsort an array of pointers to char in C? (8 个答案) 关闭 5 年前。 我尝试使用 qsort 对字符串数组进
我正在尝试重构一个当前是独立 C 程序的实用程序,以便我可以创建一个可重用的库。它包括根据全局数组中的相应值对数组进行排序的步骤。 // Global lookup table double *rat
我尝试对下面的一个struct进行排序,目的是对它们的错误率进行排序,同时保留sid和did的信息。虽然没有编译错误,但我在运行时遇到段错误。我想知道出了什么问题.... #include #inc
我正在尝试创建一个函数模板,该模板接收一个数组作为输入并对它进行排序。为了对其进行排序,我想我可以使用C++标准库的头文件qsort()中包含的cstdlib函数。 qsort()函数需要将比较器函数
#include #include #include int sortstring(const void *str1, const void *str2) { const char *r
我正在尝试对一个名为 results 的 struct run 数组进行排序,但是当我打印该数组时,没有任何排序。看看这个: struct run { char name[20], weekday
我尝试对下面的一个struct进行排序,目的是对它们的错误率进行排序,同时保留sid和did的信息。虽然没有编译错误,但我在运行时遇到段错误。我想知道出了什么问题.... #include #inc
考虑一个结构指针数组。以下代码取自您可能会找到的示例 here 。我想要为这两排铸件进行移植。我对这种“双重类型转换”不熟悉。 int myptrstructcmp(const void *p1, c
为什么我们在使用qsort()时,int(*compar)(const void*,const void*)没有添加任何参数,却也能完成它们的功能呢? 比如这样: double vals[NUM];
void qsort ( void* base, size_t num, size_t size, int (*compar)(const void*,const vo
嗨,我是学习 C 编程语言的新手,很难理解这个问题。我想对指向 struct[person] 指针数组的双指针进行排序。我需要根据多个标准对结构指针进行排序。 (年龄按升序排列,姓名和高度均按降序排列
我想对数组的特定列进行排序,但必须相应地移动其他元素例如 UNSORTED ARRAY 40 2 30 6 20 1 REQUIRED SORTED ARRAY 20 1 30 6 40 2 如果不能
假设我们有一个结构: struct product { char name[30]; float price; }; 我想首先使用 qsort 按价格对其进行排序,如果价
这个问题已经有答案了: What sorting algorithm does qsort use? (3 个回答) 已关闭 9 年前。 该功能是否qsort()在stdlib.h实际上使用快速排序算
我是一名优秀的程序员,十分优秀!