标签: qsort

Q对 malloc 的结构数组进行排序?

我在 C 语言中为我的 qsort 有这个比较器函数,但无论我尝试什么,我似乎都会遇到分段错误...

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

这是我的 qsort 调用:其中message** mList = malloc(INITIAL_CAPACITY * sizeof(message));count是一个跟踪最后一个元素的整数。message 只是一个 typedef 结构体,其中包含一个 int 和一个指向 char 的指针。我 67% 确信我正确调用了 qsort,有人能指出我正确的方向吗?

qsort (*mList, count, sizeof(message), textCompare);
Run Code Online (Sandbox Code Playgroud)

[编辑] 我声明 message*** 而不是 message* 的原因是因为我试图初始化指向结构的指针的“数组”;除非我以错误的方式处理这个问题?

c struct pointers qsort

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

为什么来自stdlib的qsort无法使用双精度值?[C]

我写了一个简单的程序来整理数组。问题在于,仅当我需要数组具有double元素时,代码才可以使用int值...有帮助吗?

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

double values[] = { 88, 56, 100, 2, 25 };

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

int main()
{
    int n;

    printf("Before sorting the list is: \n");
    for( n = 0 ; n < 5; n++ )
    {
        printf("%.2f ", values[n]);
    }

    printf("\n\n");

    qsort(values, 5, sizeof(double), cmpfunc);

    printf("\nAfter sorting the list is: \n");
    for( n = 0 ; n < 5; …
Run Code Online (Sandbox Code Playgroud)

c double qsort

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

请求成员不是结构或联合

所以我为qsort定义了函数比较,但是以下错误显示:

1.c: In function ‘compare’:
1.c:235:7: error: request for member ‘count’ in something not a structure or union
1.c:235:17: error: request for member ‘count’ in something not a structure or union
1.c:237:12: error: request for member ‘count’ in something not a structure or union
1.c:237:23: error: request for member ‘count’ in something not a structure or union
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么?我的意思是,这不像我拼错了名字:<

struct word
{
  char wordy[100];
  int count;
};


int compare(const void* a, const void* b)
{

const struct word *ia = (const …
Run Code Online (Sandbox Code Playgroud)

c struct function qsort

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

在C++中对结构的向量进行排序

我有个问题.声明说,比赛的结果是从标准输入中读取的,我必须按照已解决问题的数量按顺序在屏幕上打印最终排名.这是我的代码.

#include <cstdio>
#include <vector>
#include <cstdlib>
using namespace std;

struct results
{
  unsigned int id; //id of the team
  unsigned int m; //number of solved problems
};

int comparare(const void * i, const void * j) //compare function for qsort()
{
  return -( *(unsigned int*)i - *(unsigned int*)j );
}

int main()
{

  unsigned int n;
  vector<results> standings; //initializing an array of structs

  scanf("%u", &n); //the size of the vector
  for(unsigned int i=0; i<n; ++i)
  {
    scanf("%u%u", &standings[i].id, &standings[i].m); …
Run Code Online (Sandbox Code Playgroud)

c++ struct qsort

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

在 C 中使用 qsort 对浮点数组进行排序

最近我一直在尝试编写一个程序,它要求我对存储字符串和给定字符串的平均 ASCII 值的结构数组进行排序。我一直在尝试使用 stdlib qsort 对它进行排序,但对于编码来说相对较新,我只取得了一些成功,因为当遇到 2 个字符串的相等平均值时,我必须按字母顺序对它们进行排序,否则它们必须从从最高到最低。

我的 qsort 标头中的比较函数如下所示:

int struct_compare(const void *a, const void *b)
{
    const struct *pa;
    const struct *pb;
    pa = a;
    pb = b;
    if ( fabs(pa->average - pb->average) <= 0.000001 )
        return (strcmp(pb->text,pa->text));
    else
        return (pb->average - pa->average);
}
Run Code Online (Sandbox Code Playgroud)

对数组进行排序后,它看起来像这样:

85.166667
85.333333 
86.000000 
83.166667 
80.333333 
79.833333 
76.000000 
72.000000 
69.571429 
64.500000
Run Code Online (Sandbox Code Playgroud)

c floating-point comparison qsort

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

qsort 比较:为什么 const void *?

我一直在 Ke​​rnighan 和 Pike 合着的一本名为“编程实践”的书中学习 C 编程。根据本书中的材料,我编写了一个小程序来对命令行上给出的整数数组进行排序。

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

#define MAXSIZE 30

char *progname;
int arr[MAXSIZE];

int icmp(int *, int *);

int main(int argc, char *argv[]) {
    int i;
    progname = argv[0];
    if (argc == 1) {
        fprintf(stderr, "usage: %s [int ...]\n", progname);
        exit(1);
    }
    for (i = 0; argc > 1 && i < MAXSIZE; i++, argc--) {
        arr[i] = atoi(argv[i+1]);
    }
    int n = i;
    qsort(arr, n, sizeof(*arr), icmp);
    for (i = 0; i < n; …
Run Code Online (Sandbox Code Playgroud)

c int pointers quicksort qsort

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

C - qsort 向比较器函数发送了错误的指针?

我的问题是 qsort 似乎向比较器函数发送奇怪的指针。如果我创建 2 个间隙并将它们的指针作为参数发送,则比较器函数本身似乎工作正常。但是,在调试时,即使间隙数组已正确初始化,我也会得到错误的值。

如果重要的话,我正在 Windows 10 上运行代码。

间隙定义和比较器功能:

typedef struct open_space_t{
    ssize_t size;
    off_t start;
}Gap;

int GapComparator(const void * aa, const void * bb){
    ssize_t a = ((Gap*) aa)->size;
    ssize_t b = ((Gap*) bb)->size;

    if(a>b){
        return 1;
    }
    if(b>a){
        return -1;
    }
    else{
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

运行qsort:

Gap** allGaps = malloc((2) * sizeof(*allGaps));
allGaps[0] = malloc(sizeof(*allGaps[0]));
allGaps[0]->size = 20;
allGaps[0]->start = 30044;
allGaps[1] = malloc(sizeof(*allGaps[0]));
allGaps[1]->size = 20;
allGaps[1]->start = 30064;
qsort(allGaps, 2, sizeof(*allGaps), GapComparator);
Run Code Online (Sandbox Code Playgroud)

c debugging qsort

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

如何按照一定的顺序(不是字母顺序)对QList进行排序?

我有QList<m_User>

m_User {
    QString status;
    QString firstName;
    QString lastName;
    QDate joinDate;
    QDate leaveDate;
}
Run Code Online (Sandbox Code Playgroud)

status这里可以是:terminated, in test, requested, activated

排序顺序status应该是:activated-> terminated-> requested->in test

该 QList 应按照以下顺序排序:

  1. 状态(顺序如上)
  2. 如果状态相同,我们对名字排序,这次按字母顺序
  3. 如果名字相同,我们也按字母顺序对姓氏进行排序

所以结果应该是这样的

----------------------------------------------------------
| firstName | lastName |  status  | joinDate | leaveDate |
----------------------------------------------------------
|     A     |    C     |activated |   bla    |    bla    |
|     A     |    D     |activated |   bla    |    bla    | 
|     B …
Run Code Online (Sandbox Code Playgroud)

sorting qt qsort qlist

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

如何在C中对结构数组进行排序?

我又来了。我在尝试对c中的结构数组进行排序时遇到问题,我知道我的代码不够好,但是请不要对我不礼貌!

我尝试更改函数参数,但我已经筋疲力尽了,我敢肯定,如果我继续的话,我会做更多的错误,所以我需要您的帮助。

这是我程序的完整代码 https://pastebin.com/p28EbY8i

// I've 2 struct 

typedef struct{     // Not used in this function
    int id;
    char * nome;
    char * presidente;
    char * allenatore;
} squadra;

typedef struct{     // I've an array of this type of data
        int id;
        char * nome;
        char * cognome;
        int eta;
        char * ruolo;
        squadra team;
        char * college;
        int td;
    } giocatore;

// This is what i wrote for my function

size_t ordina_classifica(size_t sz, giocatore array[]){  //sz is the array …
Run Code Online (Sandbox Code Playgroud)

c arrays sorting struct qsort

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

Can the qsort comparison function always return a non-zero value?

An ascending sort callback function for qsort and bsearch on an array of int could look like this:

int ascending(const void *o1, const void *o2) {
    int a = *(const int *)o1;
    int b = *(const int *)o2;
    return a < b ? -1 : 1;
}
Run Code Online (Sandbox Code Playgroud)

Yet this function seems to violate the constraint on the compar function as specified in the C Standard:

7.22.5.2 The qsort function

Synopsis

#include <stdlib.h>
void qsort(void *base, size_t nmemb, size_t size,
           int …
Run Code Online (Sandbox Code Playgroud)

c sorting qsort language-lawyer

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