#include <iostream>
using namespace std;
template<typename T>
int max(T a[], int n)
{
int pos=0;
for (int i=1; i<n; i++)
{
if (a[pos] < a[i])
pos = i;
}
return pos;
}
template<typename T>
void show(T* a, int n)
{
for (int i=0; i<n; i++)
cout << a[i] << ' ';
cout << endl;
}
int main()
{
int a[4] = {1, 4, 3, 7};
double b[4] = {1.3, 4.5, 3.5, 7.8};
char c[4] = {'a', 'g', 'c', 'b'};
show(a,4);
show(b,4);
show(c,4);
cout << max(a, 4) << endl;
cout << max(b, 4) << endl;
cout << max(c, 4) << endl;
return 0;
}
模版 取得一个数组中最大元素的下标
最新推荐文章于 2023-12-24 20:52:49 发布