在C使用的MIN和MAX函数++ [英] Use of min and max functions in C++

查看:800
本文介绍了在C使用的MIN和MAX函数++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从C ++,是最大 preferable超过 FMIN 的fmax ?为了比较两个整数,做他们基本上提供相同的功能?

From C++, are min and max preferable over fmin and fmax? For comparing two integers, do they provide basically the same functionality?

你倾向于使用这些集合的功能之一,还是你preFER写自己(也许是为了提高效率,便携性,灵活性等)?

Do you tend to use one of these sets of functions or do you prefer to write your own (perhaps to improve efficiency, portability, flexibility, etc.)?

备注:

Notes:


  1. C ++标准模板库(STL)宣布在标准C的最大函数++ 算法头。

C标准(C99)提供了 FMIN 的fmax 标准C <功能HREF =htt​​p://en.wikipedia.org/wiki/Math.h> math.h中头。

The C standard (C99) provides the fmin and fmax function in the standard C math.h header.

在此先感谢!

推荐答案

FMIN 的fmax 是专门为使用浮点数(因此F)。如果你使用它的整数,你可能会根据你的编译器/平台上遭受由于转换,函数调用的开销等性能或precision损失。

fmin and fmax are specifically for use with floating point numbers (hence the "f"). If you use it for ints, you may suffer performance or precision losses due to conversion, function call overhead, etc. depending on your compiler/platform.

的std ::分钟的std ::最大是模板函数(在头的 &LT;&算法GT; )这对任何类型的工作,一个小于(&LT; )运算符,这样他们就可以在任何数据类型,允许这样的比较工作。您也可以提供自己的比较函数,如果你不希望它工作过&LT;

std::min and std::max are template functions (defined in header <algorithm>) which work on any type with a less-than (<) operator, so they can operate on any data type that allows such a comparison. You can also provide your own comparison function if you don't want it to work off <.

这是因为安全,你必须明确地转换参数相匹配时,他们有不同的类型。编译器不会让你不小心将一个64位int到64位浮点,例如。仅仅出于这个原因应该使模板默认选择。 (感谢马修M&安培; bk1e)

This is safer since you have to explicitly convert arguments to match when they have different types. The compiler won't let you accidentally convert a 64-bit int into a 64-bit float, for example. This reason alone should make the templates your default choice. (Credit to Matthieu M & bk1e)

用浮漂即使使用模板的可以在性能上胜出。编译器总是从内联源$ C ​​$ c是编译单元的一部分,模板函数的调用的选项。有时,它的无法的内联对库函数的调用,而另一方面(共享库,没有链接时优化,等等)。

Even when used with floats the template may win in performance. A compiler always has the option of inlining calls to template functions since the source code is part of the compilation unit. Sometimes it's impossible to inline a call to a library function, on the other hand (shared libraries, absence of link-time optimization, etc.).

这篇关于在C使用的MIN和MAX函数++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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