#include<iostream>
using namespace std;
template <typename T>
T maxn(T *row, int n);
template <> char *maxn<char *>(char *row[], int n);
int main()
{
int row1[] = { 1, 2, 30, 41, 5,8 };
int len1 = sizeof(row1) / sizeof(int);
int max1 = maxn(row1, len1);
cout << "For row1, we get max value= " << max1<< endl;
double row2[] = { 0.21, 1.3, 4.9, 5.2 };
int len2 = sizeof(row2) / sizeof(double);
double max2 = maxn(row2, len2);
cout << "For row2, we get max value= " << max2 << endl;
char* row[5] = { "hello", "hope", "you", "happy", "day" };
char*lo = maxn(row, 5);
cout << "We get longest words'address:" << &lo<< endl;
return 0;
}
template <typename T>
T maxn(T *row, int n)
{
T max = row[0];
for (int i = 1; i < n; i++)
{
if (max < row[i])
max = row[i];
}
return max;
}
template <> char *maxn<char *>(char *row[], int n)
{
char *lon=row[0];
for (int i = 1; i < n; i++)
if (strlen(row[i])>strlen(lon))
lon = row[i];
return lon;
}
using namespace std;
template <typename T>
T maxn(T *row, int n);
template <> char *maxn<char *>(char *row[], int n);
int main()
{
int row1[] = { 1, 2, 30, 41, 5,8 };
int len1 = sizeof(row1) / sizeof(int);
int max1 = maxn(row1, len1);
cout << "For row1, we get max value= " << max1<< endl;
double row2[] = { 0.21, 1.3, 4.9, 5.2 };
int len2 = sizeof(row2) / sizeof(double);
double max2 = maxn(row2, len2);
cout << "For row2, we get max value= " << max2 << endl;
char* row[5] = { "hello", "hope", "you", "happy", "day" };
char*lo = maxn(row, 5);
cout << "We get longest words'address:" << &lo<< endl;
return 0;
}
template <typename T>
T maxn(T *row, int n)
{
T max = row[0];
for (int i = 1; i < n; i++)
{
if (max < row[i])
max = row[i];
}
return max;
}
template <> char *maxn<char *>(char *row[], int n)
{
char *lon=row[0];
for (int i = 1; i < n; i++)
if (strlen(row[i])>strlen(lon))
lon = row[i];
return lon;
}