我一直找不到任何与此相关的问题,我想我想弄清楚这个问题有点疯狂。
\n\n我有以下代码:
\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <errno.h>\n#include <time.h>\n\nint cmp_int(const void *a, const void *b)\n{\n return * (int *)a - * (int *)b;\n}\n\nint main(int argc, char *argv[])\n{\n int n = 10;\n int **arr = calloc(n, sizeof(int *));\n srand((unsigned int) time(NULL));\n for (int i = n-1; i >= 0; i--) {\n arr[i] = calloc(1, sizeof(int));\n *(arr[i]) = rand() % 1000;\n }\n for (int i = 0; i < n; i++)\n printf("%d ", *(arr[i]));\n printf("\\n");\n qsort(arr, 10, sizeof(void *), cmp_int);\n for (int …Run Code Online (Sandbox Code Playgroud) 我有以下函数需要返回指向排序列表的指针数组
int **list_elements_sorted(int *array, int n)
{
if (n <= 0)
{
return NULL;
}
int **sorted_list = malloc(n * sizeof(int *));
assert((sorted_list != NULL) && "Error! Memory allocation failed!");
for (int i = 0; i < n; i++)
{
sorted_list[i] = &array[i];
}
qsort(sorted_list, n, sizeof(int *), comp_list_asc);
return sorted_list;
}
Run Code Online (Sandbox Code Playgroud)
和比较器功能
int comp_list_asc(const void *a, const void *b)
{
int *A = *(int **)a;
int *B = *(int **)b;
return (A - B);
}
Run Code Online (Sandbox Code Playgroud)
当我输入数组 EG 时:3 …
在搜索了很多帖子后,我无法解决我的问题。我想根据一个字段(截止日期)订购一组结构:
typedef struct{
int ident;
int computation;
int period;
int deadline;
}task_t;
task_t *tasks;
int compare(const void *a, const void *b) {
task_t *ia = *(task_t**)a;
task_t *ib = *(task_t**)b;
//task_t *ia = (task_t *)a;
//task_t *ib = (task_t *)b;
return (ia->deadline - ib->deadline);
}
//Randomly generation of parameters of tasks
fprintf(stderr,"before:\n");
for (i=0;i<nT;i++){
fprintf(stderr,"%d;%d;%d;%d\n", tasks[i].ident, tasks[i].computation, tasks[i].deadline,tasks[i].period);
}
size_t size = sizeof(tasks) / sizeof(task_t*);
qsort(tasks, size, sizeof(task_t *), compare);
fprintf(stderr,"\after:\n");
for (i=0;i<nT;i++){
fprintf(stderr,"%d;%d;%d;%d\n", tasks[i].ident, tasks[i].computation, tasks[i].deadline,tasks[i].period);
}
Run Code Online (Sandbox Code Playgroud)
在qsort之前和之后,结果是一样的。我认为问题是指针,但我不知道如何解决。我尝试了很多组合 qsort(&tasks, …
我想要qsort一个整数数组中的前 100 个元素,而其余元素保持不变。
我目前正在尝试通过以下调用来做到这一点:
int cmpfunc(const void *a, const void *b)
{
int xi = *(const int *) a;
int yi = *(const int *) b;
return (xi - yi);
}
int my_array[100+some_size];
memset(my_array, 0, sizeof(my_array));
qsort(my_array, 100, sizeof(int), cmpfunc);
Run Code Online (Sandbox Code Playgroud)
但是,我遇到了分段错误。x在 C 中可以对数组的第一个值进行排序吗?
我尝试对结构进行排序,但出现此错误:
\n\n error: cannot convert \xe2\x80\x98std::vector<Node>\xe2\x80\x99 to \xe2\x80\x98void*\xe2\x80\x99 for argument \xe2\x80\x981\xe2\x80\x99 to \xe2\x80\x98void qsort(void*, size_t, size_t, __compar_fn_t)\xe2\x80\x99\n qsort(nodes,nodes.size(), sizeof(Node), dataClustering::compare);\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的代码:\n比较函数:
\n\nint compare(const void * node1, const void * node2){\n string name1 = ((const struct Node*)node1)->name;\n string name2 = ((const struct Node*)node2)->name;\n int start1 = ((const struct Node*)node1)->start;\n int start2 = ((const struct Node*)node2)->start;\n\n if(name1 <= name2 && start1 <= start2){\n return -1;\n }\n else if(name1 > name2 && start1 > start2){\n return 1;\n }\n else{\n return 0;\n …Run Code Online (Sandbox Code Playgroud) 我有一个简单的问题,如果我有一个像这样的字符串数组:
char *array[3] = {"hello","hi","goodbye"};
如何获得数组中每个元素的大小?我正在尝试执行qsort函数,但qsorts的第三个参数需要数组中每个元素的大小(以字节为单位).谢谢
我试图排序一组无符号长.但是,由于某种原因,qsort用零填充整个数组而不是对它进行排序.我将展示对函数,比较器和GDB的调用.
此图显示了qsort之前和之后的数组,以及我对函数的调用.
这是电话:
qsort(scores[c], sizeof(scores[c]), sizeof(scores[c][0]), comparator);
Run Code Online (Sandbox Code Playgroud)
这是我的比较器函数,称为比较器:
int comparator(const void *p, const void *q)
{
if( *((unsigned long *)p) < *((unsigned long *)q)){
return -1;
}
else if( *((unsigned long *)p) == *((unsigned long *)q)){
return 0;
}
else{
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
是什么导致了这一点,我该怎么办呢?
请问功能qsort()在stdlib.h实际使用快速排序算法,顾名思义?
我需要使用qsort稳定排序数组.为了确保结果稳定,我在比较函数中添加了一个额外条件:
int compare(const void *p1, const void *p2)
{
if(*(const Data*)p1 < *(const Data*)p2)
return -1;
if(*(const Data*)p2 < *(const Data*)p1)
return 1;
else
return p1<p2 ? -1 : 1;
}
Run Code Online (Sandbox Code Playgroud)
如果qsort从不调用compare(p,p),这将有效.否则我需要使用更复杂的条件.问题是,qsort是否曾经使用重复指针调用compare(),还是总是比较不同的指针?
更新:
我用Ideone C++编译器检查了这个:https://ideone.com/l026kM
对于注释中的小例子{8,8,1,1},提供的qsort()实现不会改变指针的顺序,也不会为同一元素调用compare.这似乎是合理的,因为每次反向交换都会影响性能,因为它需要稍后进行交换.我将使用随机生成的数组和不同的编译器来测试它.
更新:
在Ideone上测试100000个随机数组,重复键的最小份额为80%.结果是100%稳定的排序数组.这是链接:https://ideone.com/KOYbgJ
VC++ Express 2008编译器无法稳定排序,因为指针的顺序已更改.这基本上说明了VC++实现与GCC实现的不同之处在于它不保持指针顺序.