[英]How to write a compare function for qsort from stdlib?
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
: 我想要做的是通过
tab_pkt.alfa
和tab_pkt.r
对tab_pkt
进行排序:
qsort(tab_pkt, ilosc_pkt, sizeof(pkt), porownaj);
Where porownaj is a compare function, but how to write it? porownaj是比较函数,但如何写呢? 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;
}
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. 返回非标准化值,令人困惑的读取,对浮点数不能正常工作,有时甚至对于整数值也不能正常工作的棘手角落情况。
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. 在机械上,您必须将const void指针转换为const结构指针。 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.
我使用显式转换--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;
}
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.
注意,通常,多维对象(例如复数,或(x,y)或(x,y,z)坐标)不能简单地比较大于或小于或等于。
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). 是的,我按alfa排序,r_kw决定pkt是否是第一个(第一个值将是最大的(或最小的)alfa和r_kw我认为)。 That's how I understand the problem, I am not 100% sure.
这就是我理解这个问题的方法,我不是百分百肯定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.