我需要在C中实现qsort并按反向词典顺序排序.我对如何创建和调用比较函数感到困惑.这是我到目前为止..
qsort (strArr, numLines, sizeof(char*) , sort);
int sort(const void * str1, const void * str2) {
return (-1) * strcasecmp((char*) str1, (char*) str2);
};
Run Code Online (Sandbox Code Playgroud)
Eclipse在qsort线上告诉我"'排序'未声明(在此函数中首次使用)",但我担心这不是我唯一的问题.有什么建议?
谢谢,Hristo
修订版...这就是我的数组的样子:
char **strArr = malloc(numLines * sizeof(char*));
fgets(output, 256, sourceFile);
strArr[i] = malloc(((int) strlen(output) + 1) * sizeof(char));
strcpy(strArr[i],output);
Run Code Online (Sandbox Code Playgroud) 根据这个网站
http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/
Run Code Online (Sandbox Code Playgroud)
我做了以下程序,对字符串进行排序
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char list[5][4]={"dat","mai","lik","mar","ana"};
int main(int argc, char *argv[])
{
int x;
puts("sortirebamde:");
for (x=0;x>sizeof(list)/sizeof(char);x++)
printf("%s\n",list[x]);
qsort(&list,(sizeof(list)/sizeof(char)),sizeof(list[0]),strcmp);
system("PAUSE");
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误
13 C:\Documents and Settings\LIBRARY\Desktop\string_sortireba.cpp invalid conversion from `int (*)(const char*, const char*)' to `int (*)(const void*, const void*)'
13 C:\Documents and Settings\LIBRARY\Desktop\string_sortireba.cpp initializing argument 4 of `void qsort(void*, size_t, size_t, int (*)(const void*, const void*))'
Run Code Online (Sandbox Code Playgroud)
请帮忙
我需要排序一个指向struc的指针数组.实际上,我需要在地址之间进行搜索,以查看数组中是否存在指向结构的指定指针.不幸的是,我在这些结构中没有任何"可比性"的东西,所以我只想按地址排序.我的代码是这样的:
item* arr[SIZE];
//something is inserted
qsort(arr, SIZE, sizeof(item*), (void*)compare_funct);
//CUT
bsearch(curr, arr, SIZE, sizeof(item*), (void*)compare_funct);
Run Code Online (Sandbox Code Playgroud)
我尝试创建一个compare_funct,只是将指针转换为int并返回它们的区别,但它似乎不起作用.特别是,当我进行bsearch时,即使我知道元素包含在数组中,我总是得到一个NULL作为返回值.
我试图通过char对结构运行数组进行排序,但是当我打印数组时,没有任何内容被排序.看看这个:
struct run {
char name[20], weekday[4], month[10];
(And some more...)
};
typedef struct run run;
int name_compare(const void *a, const void *b)
{
run *run1 = *(run **)a;
run *run2 = *(run **)b;
return strcmp(run1->name, run2->name);
}
int count_number_of_different_persons(run results[])
{
int i = 0;
qsort(results, sizeof(results) / sizeof(run), sizeof(run), name_compare);
for(i = 0; i <= 999; i++)
{
printf("%s\n", results[i].name);
}
// not done with this function yet, just return 0
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的输出只是按照最初放置的顺序列出的名称
嘿伙计们(预先警告这个问题让我觉得n00b所以我可能是),
我能够动态创建一个数组,并且我能够有效地将qsort用于静态创建的数组,但是在动态创建的数组上使用qsort时遇到了麻烦.我想我对使用指针感到磕磕绊绊.
struct my_struct {
FILE *fp;
int i;
};
Run Code Online (Sandbox Code Playgroud)
所以数组包含上面的结构,我想用int值对它进行排序.
静态地说,我可以这样做:
struct my_struct array[4];
Run Code Online (Sandbox Code Playgroud)
排序:
qsort((void *) &array, sizeof(array) / sizeof(struct my_struct), sizeof(struct my_struct), *compare);
Run Code Online (Sandbox Code Playgroud)
-
如果我这样创建数组:
struct my_struct* = malloc(sizeof(struct process) * 4);
Run Code Online (Sandbox Code Playgroud)
一切都编译并运行,但执行永远不会进入比较功能.
任何帮助将不胜感激
qsort在这里工作,但如果阵列的每个成员v占用sizeof(void *),为什么qsort期待sizeof(int)?
#include <stdio.h>
#include <stdlib.h>
int comp(const void *pa, const void *pb)
{
int a = *(int *)pa;
int b = *(int *)pb;
if (a > b)
return +1;
else
if (b > a)
return -1;
else
return 0;
}
int main(void)
{
int i, a[] = {3, 1, 2, 0, 4};
void **v;
v = malloc(sizeof(void *) * 5);
for (i = 0; i < 5; i++) {
v[i] …Run Code Online (Sandbox Code Playgroud) 我希望我简短地说清楚我在下面要做的事情.
对于SOF问题,代码非常复杂,我不认为我可以使其更简单,同时保持其他人可以直接测试.
所以我切断了相关部件并将它们放在这里.
为什么我会收到此错误,你可以帮我解决吗?
任何帮助表示赞赏!
谢谢.
char words[100][WORD_LENGTH];
char temp[WORD_LENGTH];
// scan the next %s from stream and put it to temp
while(fscanf(file, "%s", temp) > 0){
// printf("reducer reads: %s\n", temp);
strcpy(words[arr_i], temp);
printf("%d -- %s\n", arr_i, words[arr_i]);
arr_i++;
}
Run Code Online (Sandbox Code Playgroud)
在第二行我得到分段错误错误.(可能与valgrind泄漏)
int thunk = WORD_LENGTH;
qsort_r(&words, sizeof(words)/sizeof(words[0]), sizeof(words[0]), cmpstringp, &thunk);
Run Code Online (Sandbox Code Playgroud)
来自"man qsort":
static int cmpstringp(const void *p1, const void *p2) {
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers …Run Code Online (Sandbox Code Playgroud) 谁能告诉我以下示例有什么问题?我把它从这里拿走并换成int了unsigned long.我也改变了cmpfunc正确处理unsigned long.
#include <stdio.h>
#include <stdlib.h>
unsigned long values[] = { 88, 56, 100, 2, 25 };
int cmpfunc (const void * a, const void * b)
{
if(*(unsigned long*)a - *(unsigned long*)b < 0){
return -1;
}
if(*(unsigned long*)a - *(unsigned long*)b > 0){
return 1;
}
if(*(unsigned long*)a - *(unsigned long*)b == 0){
return 0;
}
}
int main()
{
int n;
printf("Before sorting the list is: …Run Code Online (Sandbox Code Playgroud) 我的教授建议我们用它qsort()来排序一系列结构.但是,我必须发现它不稳定.我知道还有其他关于这个主题的帖子,但似乎都没有提供一个简单的方法来稳定我的排序.有没有办法可以qsort()用于第二个数据字段?
这是我的代码:
#include <stdlib.h>
#include <stdio.h>
struct Map * collect_values(int n, int *arr);
void sort_values(struct Map *ptr, int n);
void print(struct Map *print_struct, int n);
struct Map{
int value, position;
};
int compare(const void *aptr, const void *bptr){
int a = ((struct Map*)aptr)->value, b = ((struct
Map*)bptr)->value;
return (a > b) - (a < b);
}
int main(){
int size, i;
scanf("%d", &size);
int *arr = (int*) malloc(size*sizeof(int));
struct Map *p = collect_values(size,arr);
qsort(p,size,sizeof(struct Map),compare);
print(p,size); …Run Code Online (Sandbox Code Playgroud) 我qsort在C中为函数编写比较函数时遇到了麻烦.这就是我目前所拥有的:
int cmpfunc(const void *a, const void *b) {
return (*(Individual*)a->fitness - *(Individual*)b->fitness);
}
Run Code Online (Sandbox Code Playgroud)
我知道比较函数是如何工作的,但我不明白如何在我的结构中引用一个整数值Individual.这是个人的结构.
typedef struct {
PPM_IMAGE image;
double fitness;
} Individual;
Run Code Online (Sandbox Code Playgroud)
我想比较结构中的适应度值.