闲来无事,试试用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. 【IOS 开发】Object - C 入门 之 数据类型详解

    1. 数据类型简介及输出() http://www.把下面的替换我.com/html/topnews201408/79/1279.htm csdn123

  2. EasyUI 的Tab 标签添加右键菜单

    样式: 主要提供右键功能代码. (只需要提供你需要的js和css就行了) <!doctype html> <html> <head> <base href=& ...

  3. STM32命名原则

    每种STM32的产品都由16个字母或数字构成的编号标示,用户向ST订货时必须使用这个编号指定需要的产品.这16个字符分为8个部分,下面通过一个例子说明它们的意义: STM32  F   103  C ...

  4. 多个div同时居中的写法

    多个div在某个div的中间,他们个数不一定但是需要在那个父级div中显示(和margin:0 auto一样的效果) <!DOCTYPE html><html lang=" ...

  5. Windows安装MySQL5.7.17

    1. 在MySQL官网 http://dev.mysql.com/downloads/mysql/ 上面下载ZIP安装包(第二个:Windows (x86, 64-bit), ZIP Archive) ...

  6. Notepad++进行php开发所必需的插件

    Notepad++进行php开发所必需的插件有那些呢? 1. Compare: 可以用来比较两个文件不同之处. 2. Explorer:文件浏览器插件,包含收藏夹.Session保存功能.可与NppE ...

  7. C++11 智能指针

    C++ 11标准库引入了几种智能指针 unique_ptr shared_ptr weak_ptr C++内存管理机制是当一个变量或对象从作用域过期的时候就会从内存中将他干掉.但是如果变量只是一个指针 ...

  8. eclipse运行项目发生Unsupported major.minor version 52.0错误

    编程的世界错误多啊 各种乱七八糟的异常数不清啊 嘿嘿呦!!! 今天又碰到莫名奇妙的问题了:eclipse中原来的项目运行的好好的,前几天想学AndroidStudio于是就装了,为了节省硬盘空间节间, ...

  9. 用Object字面量来代替swtich/if...else

    很多时候,if...else...有很多判断分支选项,就会见到: if (animal === 'dog') { // TO DO 'dog' } else if (animal === 'cat') ...

  10. Jquery基础之DOM操作

    转自:http://www.cnblogs.com/bro-ma/p/3063942.html JQuery中的DOM操作主要对包括:建[新建].增[添加].删[删除].改[修改].查[查找][像数据 ...