您的位置:首页 > 其它

两种不同写法取最小数组元素

2011-04-14 10:39 183 查看
// o414.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
using namespace std;

template <class Type,int size>
Type getMin(Type (&array)[size])
{
Type minVal = array[0];
for (int i=0;i<size;i++)
{
if (array[i]<minVal)
{
minVal = array[i];
}
}
return minVal;
}
template <class Type>
Type getMin2(Type *array,int size)
{
Type minVal = array[0];
for (int i=0;i<size;i++)
{
if (array[i]<minVal)
{
minVal = array[i];
}
}
return minVal;
}
template <class Type>
Type getMin3(const Type *array,int size)
{
Type minVal = array[0];
for (int i=0;i<size;i++)
{
if (array[i]<minVal)
{
minVal = array[i];
}
}
return minVal;
}

template <class Type>
Type myMax(Type a,Type b)
{
return a>b?a:b;
}

int _tmain(int argc, _TCHAR* argv[])
{
double myd[] = {0.3,1,2,2.2,4,8};
cout<<getMin(myd)<<endl;
cout<<getMin2(myd,sizeof(myd)/sizeof(double))<<endl;
cout<<getMin3(myd,sizeof(myd)/sizeof(double))<<endl;

//cout<<myMax(3,8)<<endl;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: