闲来无事,试试用arg_list查找n个数字中的最大者. 又因为本人喜欢模板, 所以就早早的写了以下代码, 没有经过严格测试.

/*******************************************************************************
* 版权所有:
* 模 块 名:
* 文 件 名: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;
}

  或者有人疑惑为什么对char/short/float的trait_type注释起来. 原因是因为

When you call a function with a variable argument list (which is generally a bad idea in C++), then anyfloat expressions are automatically promoted (converted) to double, and any char (any of the three flavours) and short (two flavours) are promoted to int. Therefore, as the error message says, you cannot expect va_arg() to collect a float or a char type; you need to collect a double orint and coerce the result if necessary.

  不足之处, 对于调用GetMaxValue<T>时,请确定好数据类型,并且请保证所传的比较数字的数据类型与T保持一致.若是不一致,请先转换.

查找n个数字中的最大值的更多相关文章

  1. day04_03 题目判断三个数字中的最大值

    num1 = input("Num1:") num2 = input("Num2:") num3 = input("Num3:") 输出三个 ...

  2. 提示用户一直输入数字(默认为正整数),当用户输入end的时候显示当前输入数字中的最大值。

    string input = ""; ; while (input != "end") { Console.WriteLine("请输入一个正整数,输 ...

  3. js判断三个数字中的最大值

    <script> //方法一: function maxOf3(c,d,e){ return (((c>d)?c:d)>e ? ((c>d)?c:d) : e); } c ...

  4. Excel中提取最大值的问题

    在使用excel的时候,碰到了一个如下的问题 意思是以每个字母为条件,取这个字母下面的数字中的最大值,需要注意一个问题是每个字母下面的数字个数不一定相等,例如d下面有四个数字,可以设置如下公式解决: ...

  5. SQL Server 2008 R2——查找最小nIndex,nIndex存在而nIndex+1不存在 求最小连续数组中的最大值

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  请通过右侧公告中的“联系邮 ...

  6. 一串数字中,只有一个数字出现一次,其他数字都出现两次,查找出这个数字(python)(原创)

    背景: 电话面试&手撕代码 2019.03.22 Mufasa 问题: 一串数字中,只有一个数字出现一次,其他数字都出现两次,查找出这个数字 条件: 这串数字是有序数 解决方法: 核心代码只有 ...

  7. 用matlab查找txt文档中的关键字,并把关键字后面的数据存到起来用matlab处理

    用matlab查找txt文档中的关键字,并把关键字后面的数据存到起来用matlab处理 我测了一组数据存到txt文件中,是个WIFI信号强度文档,里面有我们需要得到的数据,有没用的数据,想用matla ...

  8. sql语句查找某一列的值得最大值。

    记录一下:sql语句查找某一列的值得最大值. 1.例如我要查找 表A中a列的最大值: 查找语句可以这么写: "select Max(a) a from A" 2.查找表A中a列中包 ...

  9. 使用 sed 命令查找和替换文件中的字符串的 16 个示例

    当你在使用文本文件时,很可能需要查找和替换文件中的字符串.sed 命令主要用于替换一个文件中的文本.在 Linux 中这可以通过使用 sed 命令和 awk 命令来完成. 在本教程中,我们将告诉你使用 ...

随机推荐

  1. 不再为Apache进程淤积、耗尽内存而困扰((转))

    本篇文章是为使用Apache+MySQL,并为Apache耗尽内存而困扰的系统管理员而写.如果您没有耐心读完本文,请参考以下步骤: 修改/etc/my.cnf,加上这样一行: log-slow-que ...

  2. jQuery.last() 函数

    last() 函数详解 函数 获取当前对象的最后一个元素 语法 $selector.last() 返回值 返回值为一个对象 实例说明 代码 <!DOCTYPE html><html ...

  3. Jquery实现账单全部选中和部分选中管理

    在做购物车系统是我们往往会遇到这样一个需求,在点击全选框时我们要将全部的单个账单都选中;在单个选中账单时,如果账单全部被选中则需要全选框处于选中状态,若没有全部被选中则全选框处于没选中状态; 以下是在 ...

  4. x-csrf-token

  5. Centos 与本地终端 上传、下载 文件

    首先安装lrzsz # yum -y install lrzsz 1.上传文件,执行命令rz,会跳出文件选择窗口,选择好文件,点击确认即可. # rz 运行rz命令后弹出选择文件窗口,找到要上传的文件 ...

  6. Oracle 乱码

    导入DMP之后 ..... 1.Oacle数据库表中数据乱码 请检查导出DMP的ORACLE数据库编码设置 修改ORACLE编码与原DMP导出编码一致 select userenv('language ...

  7. [转]用CSS给SVG <use>的内容添加样式

    来源:http://www.w3cplus.com/svg/styling-svg-use-content-css.html?utm_source=tuicool&utm_medium=ref ...

  8. ACCESS中计算日均值

    如图所示,现有时间数据的时间字段是精确到时分秒的,现在需要计算PM2.5的日平均值,因此在查询时需要过滤时间字段的格式,去掉时分秒部分,只提取年月日部分. 查找资料,发现一般用CONVERT()函数实 ...

  9. Calculator(补)

    MyGitHub 刚刚开始时确实是连题目都看不懂= =,从第二行的新建类开始,就不知题目所云.所以我的困难比很多同学都要 开始得早一些--从题目第二行开始. 准确的说,当我按照题目要求新建了一个sca ...

  10. JAVA线程安全总结(转载)

    JAVA线程安全总结(一) JAVA线程安全总结(二) 最近想将java基础的一些东西都整理整理,写下来,这是对知识的总结,也是一种乐趣.已经拟好了提纲,大概分为这几个主题: java线程安全,jav ...