问题:
解答:
#include <iostream>
using namespace std;
template <typename T>
T max5(T arr[5])
{
T max = 0;
for (int i = 0; i < 5; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
return max;
}
int main()
{
int max = 0;
double max1 = 0.0;
int a1[5] = { 21,34,12,42,99 };
max=max5(a1);
cout << "int中最大的数为:" << max << endl;
double d1[5] = { 2.2,55.1,32.2,42.333,33.333 };
max1 = max5(d1);
cout << "double中最大的数为:" << max1 << endl;
return 0;
}
运行结果:
考查点:
- 模板函数
2024年9月1日21:31:353