本文介绍了用于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 itsarguments. If I don''t care about the order and simply want qsort() to run asquickly as possible, how should the comparator be defined? If qsort were stable, the following would be best: intcompare (void *x, void *y){return 0;} ....is this still true for a possibly non-stable qsort()? -trent推荐答案 在这个中,根本不调用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 thissituation? 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 notprovide a good ordering of the elements, requiring amongother things thatcompar(a,b) == -compar(b,a)andcompar(a,a) == 0andif 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 ifcompar() always returns 1 always violates the requirements of ANSI Cor not. Gordon L. Burditt Blast。 OT:有比scandir更好的功能吗?我想要一个函数 ,它接受目录的路径并返回它包含的文件名称列表 。我不在乎如何订购清单(因此之前的 问题)。 -trent Blast. OT: is there an better function than scandir? I want a functionthat takes the path of a directory and returns a list of names of filesit contains. I don''t care how the list is ordered (hence the previousquestion). -trent 我实际上并没有直接调用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比较器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-10 08:22