qsort()中的比较函数 [英] Comparison function in qsort()

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

问题描述

您好,


我在qsort()中遇到过以下比较函数用作第4个

参数


int cmp(const void * px,const void * py)

{

const double * x = px,* y = py;

返回(* x> * y) - (* x< * y);

}


它来了原样"即没有评论。


以这种方式编写比较函数是不错的风格?


返回值应该更明确吗?或者它是否足够

添加评论解释为什么功能(通常)有效?


-

问候,勺子

Hello,

I''ve come across the following comparison function used as the 4th
parameter in qsort()

int cmp(const void *px, const void *py)
{
const double *x = px, *y = py;
return (*x > *y) - (*x < *y);
}

It came "as-is" i.e. no comments.

Is it good style to write comparison functions that way?

Should the return values be made more explicit? Or would it be enough to
add comments explaining why the function (usually) works?

--
Regards, Spoon

推荐答案

Spoon schrieb:
Spoon schrieb:
你好,

我遇到过以下情况比较函数用作qsort中的第4个参数()
int cmp(const void * px,const void * py)
{
const double * x = px,* y = py;
返回(* x> * y) - (* x <* y);
}
它来了原样 ;即没有评论。

以这种方式编写比较函数是不错的风格?

返回值应该更明确吗?或者是否足以
添加注释解释函数(通常)的工作原理?
Hello,

I''ve come across the following comparison function used as the 4th
parameter in qsort()

int cmp(const void *px, const void *py)
{
const double *x = px, *y = py;
return (*x > *y) - (*x < *y);
}

It came "as-is" i.e. no comments.

Is it good style to write comparison functions that way?

Should the return values be made more explicit? Or would it be enough to
add comments explaining why the function (usually) works?




取决于。可能的观点:

1)Everyone知道C中的关系运算符返回0

或1,因此每个人都可以搞清楚。

2)如果对于Everyone来说太复杂了,那么它应该被评论。

3)如果必须要解释,以便每个人都理解它,那么它不应该以这种方式写出来 - 为了自己的利益而过于聪明。 />
4)尽可能简单明了地编写代码:

- 主机PC上的现代编译器可以更好地优化它,

- 没有危险误解

5)作为4)但是如果你通过测量cmp()是一个瓶颈来发现,那么

然后尝试将其作为一种替代方案;使用最快的替代品,并快速评论并解释你为什么做了你做的事情。

6)作为5)但考虑到可能问题是使用qsort( ),好像

;你有间接和函数调用开销。编写专用的双数组排序可能更好

。这使得上面的构造

再次变得不必要了。使用经过充分测试的书外/来自CD

算法。


您使用哪种替代方案取决于您工作的人

以及你所遵循的约束条件(编码指南等)。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



Depends. Possible points of view:
1) "Everyone" knows that relational operators in C return either 0
or 1, thus Everyone can figure it out.
2) If it is too complicated for Everyone, then it should be commented.
3) If it has to be explained so that Everyone understands it, then it
should not be written this way -- too clever for its own good.
4) Write the code as explicit and simple as possible:
- modern compilers on host PCs can optimise it better,
- there is no danger of misunderstanding
5) As 4) but if you found out by measuring that cmp() is a bottleneck,
then try this out as one alternative; use the alternative which is
fastest and comment it clearly and explain why you did what you did.
6) As 5) but consider that probably the problem is using qsort(), as
well; you have indirection and function call overhead. It may be better
to write a dedicated double array sort. This makes the above construct
once more unnecessary. Use a well-tested out-of-the-book/from-the-CD
algorithm.

Which alternative you use depends on the people you work with
and the constraints you work under (coding guidelines etc.).
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.




"匙QUOT; <无>在消息中写道

news:44 *********************** @ news.free.fr ...

"Spoon" <none> wrote in message
news:44***********************@news.free.fr...
你好,

我遇到过以下比较函数,用作qsort()中的第4个参数

int cmp(const void * px,const void * py)
{
const double * x = px,* y = py;
return(* x> * y) - (* x< * y );
}
它来了原样即没有评论。

以这种方式编写比较函数是不错的风格?

返回值应该更明确吗?或者是否足以
添加评论解释为什么功能(通常)有效?

- 问候,勺子
Hello,

I''ve come across the following comparison function used as the 4th
parameter in qsort()

int cmp(const void *px, const void *py)
{
const double *x = px, *y = py;
return (*x > *y) - (*x < *y);
}

It came "as-is" i.e. no comments.

Is it good style to write comparison functions that way?

Should the return values be made more explicit? Or would it be enough to
add comments explaining why the function (usually) works?

--
Regards, Spoon




这是(几乎)从来都不是一个好的风格来编写任何函数而没有评论

all。

大多数软件开发组都有某种编码标准。

我需要所有函数来指定函数的用途,

描述所有参数,并描述返回值。

另外这样的炫耀编码没有解释为什么它可以工作

后来经常会导致麻烦,因为其他具有较少b $ b b专业知识的人试图修改代码,或将其移植到另一种语言

(例如,布尔值true不是1)。


在上面的例子中,编码相当愚蠢。

返回(* x - * y);

同样适用,有或没有括号,

如果该函数仅用作qsort ()比较器。


如果答案必须是-1,0或+1由于某种原因(比如在需要的其他比较中使用

),它应该是

记录如此。

-

Fred L. Kleinschmidt

波音助理技术研究员

技术架构师,软件重用项目



It''s (almost) never a good style to write ANY function with no comments at
all.
Most software development groups have some sort of coding standards.
Mine require ALL functions to specify what the function is for,
describe all parameters, and describe the return value.
In addition, such show-off coding without explaining why it works
can often lead to trouble later on when someone else with less
expertise tries to modify the code, or port it to a different language
(where, for example, boolean true is not 1).

In the above case, the coding is rather silly.
return (*x - *y);
works just as well, with or without the parentheses,
if the function is used only as a qsort() comparer.

If the answer MUST be -1, 0, or +1 for some reason (like it is used
by in some other comparison that requires that), it should be
documented as such.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


" Fred Kleinschmidt" < FR ****************** @ boeing.com>写道:
"Fred Kleinschmidt" <fr******************@boeing.com> writes:
" Spoon" <无>在消息中写道
新闻:44 *********************** @ news.free.fr ...
"Spoon" <none> wrote in message
news:44***********************@news.free.fr...
您好,

我遇到过以下比较函数,用作qsort()中的第4个参数

int cmp(const void * px,const void * py)
{
const double * x = px,* y = py;
return(* x> * y) - (* x< * y);
}

它来了原样即没有评论。

以这种方式编写比较函数是不错的风格?

返回值应该更明确吗?或者是否足以添加注释来解释函数(通常)的工作原理?
Hello,

I''ve come across the following comparison function used as the 4th
parameter in qsort()

int cmp(const void *px, const void *py)
{
const double *x = px, *y = py;
return (*x > *y) - (*x < *y);
}

It came "as-is" i.e. no comments.

Is it good style to write comparison functions that way?

Should the return values be made more explicit? Or would it be enough to
add comments explaining why the function (usually) works?


[snip]在上面的例子中,编码相当愚蠢。
返回(* x - * y);
同样适用,有或没有括号,
如果该函数仅用作qsort()比较器。

[snip] In the above case, the coding is rather silly.
return (*x - *y);
works just as well, with or without the parentheses,
if the function is used only as a qsort() comparer.




不,它不会。考虑如果减法溢出会发生什么。

或者,因为操作数是double类型,考虑* x == 0.5,* y == 0.25;

结果减法为0.25,当

(隐式)转换为int时产生0.


-

Keith Thompson(The_Other_Keith ) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



No, it wouldn''t. Consider what happens if the subtraction overflows.
Or, since the operands are of type double, consider *x==0.5, *y==0.25;
the result of the subtraction is 0.25, which yields 0 when
(implicitly) converted to int.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

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