查找n个数字中的最大值
闲来无事,试试用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个数字中的最大值的更多相关文章
- day04_03 题目判断三个数字中的最大值
num1 = input("Num1:") num2 = input("Num2:") num3 = input("Num3:") 输出三个 ...
- 提示用户一直输入数字(默认为正整数),当用户输入end的时候显示当前输入数字中的最大值。
string input = ""; ; while (input != "end") { Console.WriteLine("请输入一个正整数,输 ...
- js判断三个数字中的最大值
<script> //方法一: function maxOf3(c,d,e){ return (((c>d)?c:d)>e ? ((c>d)?c:d) : e); } c ...
- Excel中提取最大值的问题
在使用excel的时候,碰到了一个如下的问题 意思是以每个字母为条件,取这个字母下面的数字中的最大值,需要注意一个问题是每个字母下面的数字个数不一定相等,例如d下面有四个数字,可以设置如下公式解决: ...
- SQL Server 2008 R2——查找最小nIndex,nIndex存在而nIndex+1不存在 求最小连续数组中的最大值
=================================版权声明================================= 版权声明:原创文章 谢绝转载 请通过右侧公告中的“联系邮 ...
- 一串数字中,只有一个数字出现一次,其他数字都出现两次,查找出这个数字(python)(原创)
背景: 电话面试&手撕代码 2019.03.22 Mufasa 问题: 一串数字中,只有一个数字出现一次,其他数字都出现两次,查找出这个数字 条件: 这串数字是有序数 解决方法: 核心代码只有 ...
- 用matlab查找txt文档中的关键字,并把关键字后面的数据存到起来用matlab处理
用matlab查找txt文档中的关键字,并把关键字后面的数据存到起来用matlab处理 我测了一组数据存到txt文件中,是个WIFI信号强度文档,里面有我们需要得到的数据,有没用的数据,想用matla ...
- sql语句查找某一列的值得最大值。
记录一下:sql语句查找某一列的值得最大值. 1.例如我要查找 表A中a列的最大值: 查找语句可以这么写: "select Max(a) a from A" 2.查找表A中a列中包 ...
- 使用 sed 命令查找和替换文件中的字符串的 16 个示例
当你在使用文本文件时,很可能需要查找和替换文件中的字符串.sed 命令主要用于替换一个文件中的文本.在 Linux 中这可以通过使用 sed 命令和 awk 命令来完成. 在本教程中,我们将告诉你使用 ...
随机推荐
- (原创)vim配色------水果色,不伤眼。
- JavaScript中的防篡改对象
由于JavaScript共享的特性,任何对象都可以被放在同一环境下运行的代码修改. 例如: var person = {name:"caibin'} person.age = 21; 即使第 ...
- jupyter nb + scite 混合搭建成我的最爱IDE
jupyter nb + scite 混合搭建成我的最爱IDE 自从体验过jupyter notebook之后, 就深深地爱上了你, jupyter. jupyter这个名字也很古怪的. 它应该是ju ...
- Sa yo na ra
总想记点些什么. 都快忘了当初是为什么来到这里呢... 2014年10月,友人给我介绍了一门编程竞赛ACM,并给我演示了一下A+B.于是我知道了ACM的含义. 2014年12月,开始水入门题. 201 ...
- /etc/ethers【地址映射】
该文件存放硬件地址和 IP 地址的映射关系. 格式如下: 00-00-00-00-00-00 0:0:0:0 每一行代表一个 IP 地址.
- angularJS 学习之路
AngularJS 通过 ng-directives 扩展了 HTML. ng-app 指令定义一个 AngularJS 应用程序.也就是angularjs作用的入口 作用在什么标签或者整个body ...
- matlab 视频转换到图像并保存
图像处理中像Adas.车辆检测等都需要采用视频文件比较好处理一点,利用帧差法.背景减法.光流法等,那么将视频文件转换到图像文件怎么做呢?话不多说,见代码一目了然: %================= ...
- [原创] Easy SysLite V1.2 (2016.5.29更新,新增加WIN10支持,一个程序适配所有系统减肥)
[原创] Easy SysLite V1.2 (2016.5.29更新,新增加WIN10支持,一个程序适配所有系统减肥) nohacks 发表于 2016-5-29 17:12:51 https:// ...
- js 截取字符串,取指定位置的字符(完善中)
1.获取字符串的最后一位或第一位 str.charAt(str.length - 1); str.charAt(0);
- call()和原型继承的方法
1.call() call()方法接受两个参数,obj和arg 比如functionA.call(obj,arg) 就是说现在运行(执行)functionA这个方法,但是functionA里面的方 ...