正确执行min [英] Correct implementation of min

查看:115
本文介绍了正确执行min的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本 D技术讨论的时间0:43:15,执行最小功能讨论。如果在某些算法中使用稳定性和额外混洗(如果值相等)的关注被提出作为所示实现的原因之一。

At time 0:43:15 in this Tech-Talk about D, The implementation of the min function is discussed. Concerns about "stability" and "extra shuffling (if values are equal)" when being used in some algorithm(s) is proposed as one of the reasons for the implementation shown.

任何人都可以提供一个真实的/实际的用例(或提供更详细的解释),这里min的具体实现是稳定(更好),而不是其他可能的实现?

Can anyone provide a real/practical use-case (or provide a more detailed explanation) where this specific implementation of min is "stable" (aka better) as opposed to its other possible implementation? Or is this just another example of alpha-geeks going a bit too far?

推荐的实施:

template <class LHS, class RHS, class Return>
inline Return min(LHS& lhs, RHS& rhs)
{
   return (rhs < lhs) ? rhs : lhs;
}

其他可能的实现:

template <class LHS, class RHS, class Return>
inline Return min(LHS& lhs, RHS& rhs)
{
   return (lhs < rhs) ? lhs: rhs;
}

提案 N2199 提供了基于后者的实现,请注意,此时建议不成功。

Proposal N2199 provides implementations that are based on the latter, please note that the proposal was not successful at this time.

与最小/最大值相关的其他相关建议是 N1840 N2485 和< a href =http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2551.pdf> N2551

Other relevant proposals relating to min/max are N1840, N2485 and N2551

推荐答案

在这种情况下,我很确定stable是指稳定,因为它应用于排序 - 即,当/如果两个元素相等,以与他们开始的相同的顺序。要实现这一点,你想要小于或等于 rhs 时返回 lhs - 但在C ++中通常)只使用运算符而不依赖于运算符<=

In this case, I'm pretty sure "stable" is referring to stable as it's applied in sorting -- i.e., that when/if two elements are equal, they stay sorted in the same order as they were to start with. To accomplish that, you want to return lhs when it's less than or equal to rhs -- but in C++ you (normally) want to do that using only operator<, without depending on having operator<=.

这篇关于正确执行min的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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