标签: qsort

C中的bsearch函数

如果我有两个函数:

void SortStudents(char *studentList[], size_t studentCount) 
{
    qsort(studentList, sizeof(studentList)/sizeof(studentList[0]), sizeof(studentList[0]), Compare);
}

int Compare(const void *a, const void *b) 
{
    return (strcmp(*(char **)a, *(char **)b));
}
Run Code Online (Sandbox Code Playgroud)

使用 qsort 函数进行排序和比较,如何使用 bsearch 查找列表的子集。例如,如果我有两个列表:

  • (列表 A)鲍勃、吉米、李、詹姆斯、安妮
  • (名单 B)Jen、Jon、Lee、James、Steph

如何在列表 B 中搜索以找到 A 中的那些元素?

您是否也可以在列表 B 中进行搜索以查找 A 中没有的元素?

谢谢。

c qsort bsearch

1
推荐指数
1
解决办法
3064
查看次数

看来我的qsort没有给出正确的结果为什么?

这是我的比较功能:

   int compare (const void * a, const void * b)
    {
        ptnode * ia = (ptnode*)a;
        ptnode * ib = (ptnode*)b;
        return (int)(100.f*ia->x - 100.f*ib->x );
    }
Run Code Online (Sandbox Code Playgroud)

我打电话给qsort:

qsort(sortbase,index,sizeof(ptnode),compare);
Run Code Online (Sandbox Code Playgroud)

sortbase是我的struct ptnode的数组,定义如下:

typedef struct node
{
    struct node  *pre1;
    struct node  *pre2;
    struct node  *pre;
    double          x;
    double y;
    double maxlength;
} ptnode;
Run Code Online (Sandbox Code Playgroud)

sortbase是这样的:

struct node * sortbase[1000];
Run Code Online (Sandbox Code Playgroud)

我想按x值对它们进行排序,但在qsort之前和之后,没有任何改变,

为什么?提前致谢.

c qsort

1
推荐指数
1
解决办法
151
查看次数

C中的qsort段错误

我正在尝试根据手册页使用 qsort,但无论我尝试什么,我都会遇到段错误

这是重要的代码部分

int compare_dirent(const void *a, const void *b)
{
    const struct dirent *first = (const struct dirent *) a;
    const struct dirent *second = (const struct dirent *) b;

    return first->d_ino - second->d_ino;
}


int process(FILE* output,const char *dirname, int flags)
{
    struct dirent *entries = NULL;
    struct dirent *table[256];
    int entry_num = 0;
    DIR *directory = NULL;
    char cwd[1024];

    getcwd(cwd,1024);
    bzero(table,256);

    directory = opendir(dirname);
    while((entries = readdir(directory))!=NULL)
    {
        if(entries->d_type == DT_REG)
        {
            fprintf(output,"%s\t\n",entries->d_name);
            table[entry_num] = entries;
            entry_num++; …
Run Code Online (Sandbox Code Playgroud)

c qsort segmentation-fault

1
推荐指数
1
解决办法
4094
查看次数

如何在c中将比较函数与typedef进行比较

我正在创建一个比较函数来使用qsort()但我无法正确地转换元素.我试过不同的东西,但它永远不会奏效.有人可以解释一下这样做的正确逻辑吗?先感谢您!

typedef struct _stringa {
    char* string;
    int freq;
} stringa;


int compare(const void *elem1, const void *elem2) {

    if (*(stringa*)elem1.freq < *(stringa*)elem2.freq) {
        return -1;
    } else if (*(stringa*)elem1.freq > *(stringa*)elem2.freq) {
        return 1;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑:对不起第二个问题,但我的排序不起作用,它似乎'删除'元素.这是调用qsort的正确方法吗?

qsort(ARRAY, ARRAY_DIMENSION, sizeof(struct _stringa), compare);
Run Code Online (Sandbox Code Playgroud)

c struct qsort

1
推荐指数
1
解决办法
321
查看次数

在 C 中将 const void * 转换为 const char *

所以我有一个 C 函数,如下所示:

int cmp (const void *a, const void* b)
 return rot13cmp( (const char*)a, (const char*)b );
}
Run Code Online (Sandbox Code Playgroud)

rot13cmp 是另一个函数,它采用两个 const char * 类型的参数。

我将此函数传递给 C qsort函数的比较参数,但它似乎不起作用。

但是,如果我通过执行以下操作来转换 const void * 变量

return rot13cmp ( *(const char **)a, *(const char **)b ); 
Run Code Online (Sandbox Code Playgroud)

然后该功能开始工作。我查了一下这个,但每个消息来源都说第一种铸造方法应该有效,所以我想知道为什么只有第二种方法对我有用?

编辑:这是我的相关代码,

int cmp (const void *a, const void *b) {
 return rot13cmp( (const char *)a, (const char *)b );
}

int rot13cmp (const char *a, const char *b) {
 while (*a == *b && *a …
Run Code Online (Sandbox Code Playgroud)

c casting qsort

1
推荐指数
1
解决办法
6370
查看次数

尝试运行 Anagram 时出现警告(John Bentley-Programming Pearls)-C

对 C 完全陌生。只是想通过运行 John Bentley 的 Anagram(我相信是第 2 列)程序来掌握 Linux 和 C 编程的窍门。很确定我逐字复制了这段代码(必须添加标头等),但我收到一条警告,当使用我的 squash.c 程序编译和运行时,会给出不需要的输出。承认吧,我什至不知道这个 charcomp 函数的行为方式,或者它的作用。(那里的一些启发也很好)。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int charcomp(char *x, char *y) {return *x - *y;}

#define WORD_MAX 100
int main(void)
{
        char word[WORD_MAX], sig[WORD_MAX];
        while (scanf("%s", word) != EOF) {
                strcpy(sig, word);
                qsort(sig, strlen(sig), sizeof(char), charcomp);
                printf("%s %s\n", sig, word);
        }
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是警告。

sign.c:13:41: warning: incompatible pointer types passing 'int (char *, char *)'
      to parameter of type '__compar_fn_t' (aka 'int …
Run Code Online (Sandbox Code Playgroud)

c qsort

1
推荐指数
1
解决办法
611
查看次数

qsort结构数组降序排列

我有一个看起来像这样的结构

typedef struct id_score{
   int id;
   int score;
} pair;
Run Code Online (Sandbox Code Playgroud)

大小为50的数组保存指向这些对的指针

pair* array[50]
Run Code Online (Sandbox Code Playgroud)

我的比较器功能看起来像这样

int struct_cmp(const void *a, const void* b) {
    pair* ia = (pair*)a;
    pair* ib = (pair*)b;
    printf("ia's score: %d ib's score: %d??? \n", ia->score, ib->score);
    return ib->score - ia->score;
}
Run Code Online (Sandbox Code Playgroud)

我的qsort功能在这里

size_t arr_len = sizeof(array) / sizeof(pair);
qsort(array, arr_len, sizeof(pair), struct_cmp);
Run Code Online (Sandbox Code Playgroud)

现在我的问题是,在struct_cmp函数中,我printf表明我认为应该是数组中每个结构中的计数的值都被解释为0,因此根本不对数组进行排序.在我通过数组打印时,函数外部的结构具有应该具有的分数.

有什么建议??谢谢!

c arrays struct qsort comparator

1
推荐指数
1
解决办法
583
查看次数

需要对动态的指针数组进行排序,并在其中包含可能的NULL指针

我试图弄清楚如何对结构中的指针数组进行排序,其中包含可能的空指针.但我无法理解它,并在排序后不断崩溃.

我有两个结构,CAR并且CARLIST: CARLIST有一个指针数组CARS.而我无法做到对.

谢谢你的帮助...

typedef struct Car {
    int parked_Total_Minutes;
    char rz[10];
} CAR;

typedef struct CarList {
    CAR **p_cars;
    unsigned int count;
    unsigned int size;
} CARLIST;

int Compare_ParkedTime(const void *a, const void *b) {
     if (a == NULL)
          return -1;

     if (b == NULL)
          return 1;

     CAR *aa = *(CAR* const *)a;
     CAR *bb = *(CAR* const *)b;

     return  (bb->parked_Total_Minutes < aa->parked_Total_Minutes) - (aa->parked_Total_Minutes < bb->parked_Total_Minutes);
}

int main() {
    .... …
Run Code Online (Sandbox Code Playgroud)

c struct pointers qsort

1
推荐指数
1
解决办法
98
查看次数

为什么 qsort int 数组降序排列不正确?

我尝试编写比较函数来对 int 数组进行排序。它给出了升序的正确结果。但对于降序排列是不正确的。为什么?什么是正确的比较 int 函数?

int compare(const void *a, const void *b){
    int x = *(int*)a;
    int y = *(int*)b;
    return (x > y) - (x < y);
}

int reverse(const void *a, const void *b){
    return -compare(a, b);
}

int main(){
    int x[] = {500, 456, 18, 13, 3, 89, 800, 6874};
    qsort(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), reverse);
    for (int i=0; i < sizeof(x)/sizeof(x[0]); i++){
    printf("%d\n", x[i]);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我期望打印的整数按降序排列,从最大的 6874 到最小的 3。但我收到的却是:

800 6874 500 456 18 13 3 …

c qsort

1
推荐指数
1
解决办法
1002
查看次数

qsort 创建非特定数量的零

所以我的程序从文件中读取整数,同时跳过以 开头的行,#然后将它们存储在一个数组中并使用该qsort函数将它们排序打印出来。但是,当它们被打印时,由于某种原因,它在开始时会打印一些零,然后是排序后的数字。如果数字对是 60,我得到 21 个零,如果数字对是 103688,它打印 60151 个零。

输入文件:

#Skip
#These
#Lines
25  8
25  19
25  23
25  28
25  29
25  30
25  33
25  35
25  50
25  54
25  55
Run Code Online (Sandbox Code Playgroud)

该计划是:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


typedef struct {
    int start;
    int end;   
} path;

int cmp(const void *a,const void *b){
    int l=((path*)a)->start;
    int r=((path*)b)->start;

    if(l>r)
        return 1;
    if(l<r)
        return -1;
    if(l==r)
        return 0;
}

int doublesize(path** array,int n){
    path* new_array=malloc(n*2*sizeof(path));
    if(new_array==NULL){
        printf("Error allocating …
Run Code Online (Sandbox Code Playgroud)

c zero qsort

1
推荐指数
1
解决办法
59
查看次数

标签 统计

c ×10

qsort ×10

struct ×3

arrays ×1

bsearch ×1

casting ×1

comparator ×1

pointers ×1

segmentation-fault ×1

zero ×1