用arg_list查找n个数字中的最大者

      今天看到一篇小程序,用arg_list查找n个数字中的最大者,发现作者写的代码不够简单。类型定义的地方太多了,而且无法编译通过。本人进行了修正。原文代码(http://www.cnblogs.com/xiao13149920yanyan/archive/2013/11/07/3412398.html#commentform)为:

/******************************************************************************* 
* 版权所有: 
* 模 块 名: 
* 文 件 名:template_max_value.cpp 
* 实现功能:取不定数目的数值中的最大值 
* 作    者:leaker 
* 版    本:V1.0 
* 日    期:2013.11.07 
* 联系方式:xiao13149920@foxmail.com 
********************************************************************************/
// template_max_value.cpp 
#include<stdarg.h> 
#include<iostream> 
using namespace std; 
  
template<typename T> 
struct trait_type; 
#if 0 
template<> 
struct trait_type<char> 
{ 
    typedef char value_type; 
}; 
  
template<> 
struct trait_type<unsigned char> 
{ 
    typedef unsigned char value_type; 
}; 
  
template<> 
struct trait_type<short> 
{ 
    typedef short value_type; 
}; 
  
template<> 
struct trait_type<unsigned short> 
{ 
    typedef unsigned short value_type; 
}; 
#endif 
template<> 
struct trait_type<int> 
{ 
    typedef int value_type; 
}; 
  
template<> 
struct trait_type<unsigned int> 
{ 
    typedef unsigned int value_type; 
}; 
  
template<> 
struct trait_type<long> 
{ 
    typedef long value_type; 
}; 
  
template<> 
struct trait_type<unsigned long> 
{ 
    typedef unsigned long value_type; 
}; 
  
template<> 
struct trait_type<long long> 
{ 
    typedef long long value_type; 
}; 
  
template<> 
struct trait_type<unsigned long long> 
{ 
    typedef unsigned long long value_type; 
}; 
#if 0 
template<> 
struct trait_type<float> 
{ 
    typedef float value_type; 
}; 
#endif 
template<> 
struct trait_type<double> 
{ 
    typedef double value_type; 
}; 
  
template <typename T> 
typename trait_type<T>::value_type GetMaxValue(int numarg, ...) 
{ 
    va_list args; 
    va_start(args, numarg); 
    typedef typename trait_type<T>::value_type value_type; 
    value_type ret = va_arg(args, value_type); 
    while(--numarg) 
    { 
        value_type arg = va_arg(args, value_type); 
        (ret < arg) &&  (ret = arg); 
    } 
    va_end(args); 
  
    return ret; 
} 
  
int main() 
{ 
    int a = 9; 
    int b = 19; 
    int c = 29; 
    int d = 39; 
  
    std::cout<<GetMaxValue<int>(12,c,d,1,2,3,4,5,6,7,8,9,static_cast<int>(199.990))<<std::endl;  
    std::cout<<GetMaxValue<double>(10 ,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.9,199.990)<<std::endl; 
    std::cout<<GetMaxValue<unsigned int>(12,c,d,1,2,3,4,5,6,7,8,9,static_cast<unsigned int>(199.990))<<std::endl;  
  
    return 0; 
} 


我改为:

#include<stdarg.h> 
#include<iostream> 
using namespace std; 
  
 template<typename T> 
struct trait_type 
 {
    typedef T value_type;
 };

 
template<typename T> 
typename trait_type<T>::value_type GetMaxValue(int numarg, ...) 
{ 
    va_list args; 
    va_start(args, numarg); 
    typedef typename trait_type<T>::value_type value_type; 
    value_type ret = va_arg(args, value_type); 
    while(--numarg) 
    { 
        value_type arg = va_arg(args, value_type); 
        (ret < arg) &&  (ret = arg); 
    } 
    va_end(args); 
  
    return ret; 
} 

 
int main() 
{ 
    int a = 9; 
    int b = 19; 
    int c = 29; 
    int d = 39; 
  
   std::cout<<GetMaxValue<int>(12,c,d,1,2,3,4,5,6,7,8,9,static_cast<int>(199.990))<<std::endl;  
    std::cout<<GetMaxValue<double>(10 ,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.9,199.990)<<std::endl; 
    std::cout<<GetMaxValue<unsigned int>(12,c,d,1,2,3,4,5,6,7,8,9,static_cast<unsigned int>(199.990))<<std::endl;  
  
    return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值