用于qsort的noop比较器 [英] noop comparator for qsort

查看:84
本文介绍了用于qsort的noop比较器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第四个参数是一个比较器,它返回一个小于的整数,

等于或大于零''取决于它的

参数的顺序。


如果我不在乎订单并且只想让qsort()尽可能快地以
运行,那么应该如何定义比较器?


如果qsort稳定,以下情况最好:


int

compare(void * x, void * y)

{

返回0;

}


....是这仍然适用于可能不稳定的qsort()?

-trent

The fourth argument is a comparator that returns `an integer less than,
equal to, or greater than zero'' depending on the ordering of its
arguments.

If I don''t care about the order and simply want qsort() to run as
quickly as possible, how should the comparator be defined?

If qsort were stable, the following would be best:

int
compare (void *x, void *y)
{
return 0;
}

....is this still true for a possibly non-stable qsort()?

-trent

推荐答案

>第四个参数是一个比较器,它根据其参数的顺序返回一个小于,
>The fourth argument is a comparator that returns `an integer less than,
等于或大于零的整数。
如果我不在乎订单并且只是希望qsort()尽可能快地运行,那么比较器应该如何?界定?


如果你想让它快速运行,根本不要打电话给qsort()。

如果qsort稳定,以下情况最好:

int
比较(void * x,void * y)
{
返回0;
}

.. 。对于一个可能不稳定的qsort(),这仍然是正确的吗?
equal to, or greater than zero'' depending on the ordering of its
arguments.

If I don''t care about the order and simply want qsort() to run as
quickly as possible, how should the comparator be defined?
If you want it to run quickly, don''t call qsort() at all.
If qsort were stable, the following would be best:

int
compare (void *x, void *y)
{
return 0;
}

...is this still true for a possibly non-stable qsort()?




在这个中,根本不调用qsort()会更快吗? />
情况?你似乎没有改变任何东西就好了。


某些版本的qsort()已被证明行为不端

(例如,涂抹错误)比较例程没有

提供了一个良好的元素排序,要求

其他的东西

比较(a,b)== -compar(b,a)



比较(a,a)== 0



如果比较(a,b)> 0和比较(b,c)> 0,那么比较(a,c)必须是> 0


我不确定qsort()是否走出数组的末尾如果

compar()总是返回1总是违反ANSI C的要求

与否。


Gordon L. Burditt



Wouldn''t it be faster to not call qsort() at all in this
situation? You seem to be OK with having it not change anything.

Some versions of qsort() have been proven to misbehave badly
(e.g. smegmentation fault) if the comparison routine does not
provide a good ordering of the elements, requiring among
other things that
compar(a,b) == -compar(b,a)
and
compar(a,a) == 0
and
if compar(a,b) > 0 and compar(b,c) > 0, then compar(a,c) must be > 0

I am not sure whether qsort() walking off the end of the array if
compar() always returns 1 always violates the requirements of ANSI C
or not.

Gordon L. Burditt


Quoth Gordon Burditt 2004年左右 - 11-14:
Quoth Gordon Burditt on or about 2004-11-14:
在这种情况下根本不打电话给qsort()会不会更快?你似乎没有改变任何东西。


我实际上并没有直接调用qsort。我在叫scandir;我想是

如果我提到这个我会被告知`走开,这是clc,而不是

cpunix'':-)

某些版本的qsort()已被证明行为不当
Wouldn''t it be faster to not call qsort() at all in this
situation? You seem to be OK with having it not change anything.
I''m not actually calling qsort directly. I''m calling scandir; I figured
if I mentioned this I would be told `Go away, this is c.l.c, not
c.p.unix'' :-)
Some versions of qsort() have been proven to misbehave badly




Blast。 OT:有比scandir更好的功能吗?我想要一个函数

,它接受目录的路径并返回它包含的文件名称列表

。我不在乎如何订购清单(因此之前的

问题)。


-trent



Blast. OT: is there an better function than scandir? I want a function
that takes the path of a directory and returns a list of names of files
it contains. I don''t care how the list is ordered (hence the previous
question).

-trent


Trent Buck写道:
Trent Buck wrote:

Quoth Gordon Burditt在2004-11-14左右:

Quoth Gordon Burditt on or about 2004-11-14:
不是更快在这种情况下完全打电话给qsort()?你似乎没有改变任何事情就好了。
Wouldn''t it be faster to not call qsort() at all in this
situation? You seem to be OK with having it not change anything.



我实际上并没有直接调用qsort。
我正在调用scandir;我想如果我提到这个我会被告知`走开,这是clc,而不是cpunix'': - )



I''m not actually calling qsort directly.
I''m calling scandir; I figured
if I mentioned this I would be told `Go away, this is c.l.c, not
c.p.unix'' :-)




你不喜欢cpunix吗?


-

pete



You don''t like c.p.unix?

--
pete


这篇关于用于qsort的noop比较器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆