如何解决numeric_limits< T> :: min()的不一致定义? [英] How to workaround the inconsistent definition of numeric_limits<T>::min()?

查看:230
本文介绍了如何解决numeric_limits< T> :: min()的不一致定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numeric_limits traits应该是获取各种类型信息的一般方式,以便能够执行

The numeric_limits traits is supposed to be a general way of obtaining various type infomation, to be able to do things like

template<typename T>
T min(const std::vector<T>& vect)
{
    T val = std::numeric_limits<T>::min();

    for(int i=0 ; i<vect.size() ; i++)
        val = max(T, vect[i]);

    return val;
}


$ b < ; :: min()返回最小的负数,而numeric_limits< double> :: min()返回最小的数字!

任何人都知道这个设计的理由吗?是否有更好的(推荐?)使用numeric_limits的方法?在我上面的具体函数中,我当然可以初始化T到vect [0],但这不是我正在寻找的答案..

Anyone knows the rationalie behind this design? Is there a better (recommended?) way of using numeric_limits? In my specific function above, I could of course initialize T to vect[0], but that is not the answer I am looking for..

点特定)讨论
这里

推荐答案

您可以使用Boost库。

You can use Boost libraries. The library Numeric Conversions provides a class called bounds that can be used consistently.

请参阅文档此处

这篇关于如何解决numeric_limits&lt; T&gt; :: min()的不一致定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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