如何从 stdlib 编写 qsort 的比较函数?

发布于 2024-07-09 17:37:18 字数 778 浏览 6 评论 0原文

我有一个结构:

struct pkt_
{
  double x;
  double y;
  double alfa;
  double r_kw;
};

typedef struct pkt_ pkt;

这些结构的表格:

pkt *tab_pkt;

tab_pkt = malloc(ilosc_pkt * sizeof(pkt));

我想要做的是按 tab_pkt.alfatab_pkt.rtab_pkt 进行排序:

qsort(tab_pkt, ilosc_pkt, sizeof(pkt), porownaj);

其中porownaj是比较函数,但是怎么写呢? 这是我的“草图”:

int porownaj(const void *pkt_a, const void *pkt_b)
{
  if (pkt_a.alfa > pkt_b.alfa && pkt_a.r_kw > pkt_b.r_kw) return 1;
  if (pkt_a.alfa == pkt_b.alfa && pkt_a.r_kw == pkt_b.r_kw) return 0;
  if (pkt_a.alfa < pkt_b.alfa && pkt_a.r_kw < pkt_b.r_kw) return -1;
}

I have a structure:

struct pkt_
{
  double x;
  double y;
  double alfa;
  double r_kw;
};

typedef struct pkt_ pkt;

A table of these structures:

pkt *tab_pkt;

tab_pkt = malloc(ilosc_pkt * sizeof(pkt));

What I want to do is to sort tab_pkt by tab_pkt.alfa and tab_pkt.r:

qsort(tab_pkt, ilosc_pkt, sizeof(pkt), porownaj);

Where porownaj is a compare function, but how to write it? Here is my "sketch" of it:

int porownaj(const void *pkt_a, const void *pkt_b)
{
  if (pkt_a.alfa > pkt_b.alfa && pkt_a.r_kw > pkt_b.r_kw) return 1;
  if (pkt_a.alfa == pkt_b.alfa && pkt_a.r_kw == pkt_b.r_kw) return 0;
  if (pkt_a.alfa < pkt_b.alfa && pkt_a.r_kw < pkt_b.r_kw) return -1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋顶上的小猫咪 2024-07-16 17:37:18

像这样的东西应该有效:

int porownaj(const void *p_a, const void *p_b)
{
  /* Need to store arguments in appropriate type before using */
  const pkt *pkt_a = p_a;
  const pkt *pkt_b = p_b;

  /* Return 1 or -1 if alfa members are not equal */
  if (pkt_a->alfa > pkt_b->alfa) return 1;
  if (pkt_a->alfa < pkt_b->alfa) return -1;

  /* If alfa members are equal return 1 or -1 if r_kw members not equal */
  if (pkt_a->r_kw > pkt_b->r_kw) return 1;
  if (pkt_a->r_kw < pkt_b->r_kw) return -1;

  /* Return 0 if both members are equal in both structures */
  return 0;
}

远离愚蠢的技巧,例如:

return pkt_a->r_kw - pkt_b->r_kw;

返回非标准化值,阅读起来令人困惑,对于浮点数无法正常工作,有时会出现棘手的极端情况,即使对于整数也无法正常工作价值观。

Something like this should work:

int porownaj(const void *p_a, const void *p_b)
{
  /* Need to store arguments in appropriate type before using */
  const pkt *pkt_a = p_a;
  const pkt *pkt_b = p_b;

  /* Return 1 or -1 if alfa members are not equal */
  if (pkt_a->alfa > pkt_b->alfa) return 1;
  if (pkt_a->alfa < pkt_b->alfa) return -1;

  /* If alfa members are equal return 1 or -1 if r_kw members not equal */
  if (pkt_a->r_kw > pkt_b->r_kw) return 1;
  if (pkt_a->r_kw < pkt_b->r_kw) return -1;

  /* Return 0 if both members are equal in both structures */
  return 0;
}

Stay away from silly tricks like:

return pkt_a->r_kw - pkt_b->r_kw;

which return un-normalized values, are confusing to read, won't work properly for floating point numbers, and sometimes have tricky corner cases that don't work properly even for integer values.

北风几吹夏 2024-07-16 17:37:18

问题分为两部分——如何编写代码,以及如何比较数据包类型。 您必须确保始终返回一个值。 您的代码还应该始终是这样的:

porownaj(&pkt_a, &pkt_b) == -porownaj(&pkt_b, &pkt_a)

您的大纲比较不处理以下情况:

pkt_a->alfa >  pkt_b->alfa && pkt_a->r_kw <= pkt_b->r_kw
pkt_a->alfa <  pkt_b->alfa && pkt_a->r_kw >= pkt_b->r_kw
pkt_a->alfa == pkt_b->alfa && pkt_a->r_kw != pkt_b->r_kw

还有一个问题 - 比较浮点值是否完全相等? 这将取决于您的应用程序。

从机械上讲,您必须将 const void 指针转换为 const 结构指针。 我使用显式强制转换 - C++ 需要它,并且我尝试使我的代码可以被 C++ 编译器接受,即使它实际上是 C 代码。

int porownaj(const void *vp1, const void *vp2)
{
     const pkt *pkt_a = (const pkt *)vp1;
     const pkt *pkt_b = (const pkt *)vp2;

     if (pkt_a->alfa >  pkt_b->alfa && pkt_a->r_kw >  pkt_b->r_kw) return 1;
     if (pkt_a->alfa == pkt_b->alfa && pkt_a->r_kw == pkt_b->r_kw) return 0;
     if (pkt_a->alfa <  pkt_b->alfa && pkt_a->r_kw <  pkt_b->r_kw) return -1;
     return 0;
 }

这不涉及我无法解决的问题,因为我不了解必要的信息。 请注意,一般来说,多维对象(例如复数,或 (x,y) 或 (x,y,z) 坐标)不能简单地比较大于或小于或等于。

There are two parts to the problem - how to write the code, and how to compare the packet types. You must ensure you always return a value. Your code should also always be such that:

porownaj(&pkt_a, &pkt_b) == -porownaj(&pkt_b, &pkt_a)

Your outline comparison does not handle cases such as:

pkt_a->alfa >  pkt_b->alfa && pkt_a->r_kw <= pkt_b->r_kw
pkt_a->alfa <  pkt_b->alfa && pkt_a->r_kw >= pkt_b->r_kw
pkt_a->alfa == pkt_b->alfa && pkt_a->r_kw != pkt_b->r_kw

There is one more problem - is it appropriate to compare floating point values for exact equality? That will depend on your application.

Mechanically, you have to convert the const void pointers to const structure pointers. I use the explicit cast - C++ requires it, and I try to make my code acceptable to a C++ compiler even when it is really C code.

int porownaj(const void *vp1, const void *vp2)
{
     const pkt *pkt_a = (const pkt *)vp1;
     const pkt *pkt_b = (const pkt *)vp2;

     if (pkt_a->alfa >  pkt_b->alfa && pkt_a->r_kw >  pkt_b->r_kw) return 1;
     if (pkt_a->alfa == pkt_b->alfa && pkt_a->r_kw == pkt_b->r_kw) return 0;
     if (pkt_a->alfa <  pkt_b->alfa && pkt_a->r_kw <  pkt_b->r_kw) return -1;
     return 0;
 }

This does not deal with the bits that I cannot resolve since I am not party to the necessary information. Note that, in general, multi-dimensional objects (such as complex numbers, or (x,y) or (x,y,z) coordinates) cannot simply be compared for greater than or less than or equal to.

魔法少女 2024-07-16 17:37:18

是的,我按 alfa 排序,r_kw 决定 pkt 是否是第一个(我认为第一个值将具有最大(或最小)的 alfa 和 r_kw)。 这就是我对这个问题的理解,我不是100%确定。

Yes, I am sorting by alfa and r_kw decides if pkt is first (first value will have the biggest (or smallest) alfa and r_kw I think). That's how I understand the problem, I am not 100% sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文