Correct implementation of min

南笙酒味 提交于 2019-11-27 21:20:10

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<=.

In general case there is no advantage to one implementation over the other. If you're implementing min for a specific usage, it might make sense to choose one of the forms based on the data to which it will be applied to make the most of branch predictions.

If for most usage cases the expected minimum is rhs, choose the first implementation. If it's lhs, choose the second implementation.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!