// CPPTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include<fstream>
#include <iostream>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
template <class T>
T maxn(T *arr,int n) {
T max=arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
template<>
const char * maxn(const char *arr[], int n) {
const char *p = arr[0];
for (int i = 1; i < n; i++) {
if (strcmp(p, arr[i]) < 0) {
p = arr[i];
}
}
return p;
}
int main() {
int arri[6] = { 3,4,6,2,1,8 };
double arrd[4] = { 1.1,2.2,3.3,4.4};
const char * arrc[4] = { "Hello","How are you","Amercian","Zenith" };
int max_i = maxn(arri,6);
double max_d = maxn(arrd,4);
const char *p = maxn(arrc, 4);
cout << "max i=" << max_i << endl;
cout << "max d=" << max_d << endl;
cout << "max string=";
for (; *p != 0; p++)
cout << *p;
cout << endl;
}
标签:arr,const,int,max,char,maxn,重载,例题,模板
From: https://www.cnblogs.com/zhongta/p/18325432