第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C++中的qsort問題

C++中的qsort問題

C++
靜以修身淡以明志 2015-11-26 01:18:18
#include <stdio.h>#include <stdlib.h>struct c{ ? ?int x; ? ?int y; ? ?int ord;}d[100];int cmp(const struct c *a, const struct c *b){ ? ?if ((*a).x == (*b).x) ? ? ? ?return (*a).y - (*b).y; ? ?else ? ? ? ?return (*a).x - (*b).x;}int main(void){ ? ?int i, j, n, max; ? ?while (scanf("%d", &n), n) ? ?{ ? ? ? ?for (max = i = 0; i < n; i++) ? ? ? ?{ ? ? ? ? ? ?scanf("%d%d", &d[i].x, &d[i].y); ? ? ? ? ? ?d[i].ord = 1; ? ? ? ?} ? ? ? ?qsort(d, n, sizeof(struct c), cmp); ? ? ? ?d[n-1].ord = 1; ? ? ? ?for (i = n - 2; i >= 0; i--) ? ? ? ?{for (j = i + 1; j < n; j++) ? ? ? ? ? ?{ ? ? ? ? ? ? ? ?if (d[i].y <= d[j].x && d[i].ord < d[j].ord + 1) ? ? ? ? ? ? ? ? ? ?d[i].ord = d[j].ord + 1; ? ? ? ? ? ?} ? ? ? ? ? ?if (max < d[i].ord) ? ? ? ? ? ? ? ?max = d[i].ord; ? ? ? ?} ? ? ? ?printf("%d\n", max); ? ?} ? ?return 0;}這段程序在VC6下無法通過編譯,是CMP函數(shù)的參數(shù)問題我改成cmp(void *a,void *b)這種形式也不行也通不過,提示函數(shù)參數(shù)不匹配error C2664: 'qsort' : cannot convert parameter 4 from 'int (const struct c *,const struct c *)' to 'int (__cdecl *)(const void *,const void *)'該怎樣修改呢?
查看完整描述

1 回答

已采納
?
DoDream

TA貢獻(xiàn)28條經(jīng)驗 獲得超3個贊

因為你的參數(shù)設(shè)計有問題,應(yīng)該按照要求設(shè)為const void *,然后在函數(shù)中再轉(zhuǎn)換成對應(yīng)的結(jié)構(gòu)體指針。

int cmp(const void *a, const void *b)

{

const struct c* a1 = (struct c*)a;

const struct c* b1 = (struct c*)b;

if ((*a1).x == (*b1).x)

return (*a1).y - (*b1).y;

else

return (*a1).x - (*b1).x;

}

我將你的cmp函數(shù)改了一下,編譯通過了,你試試看行不行。

查看完整回答
反對 回復(fù) 2015-11-26
  • 1 回答
  • 0 關(guān)注
  • 2068 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號