函数重用和函数引用结合测试

来源:互联网 发布:淘宝大学商学院杭州 编辑:程序博客网 时间:2024/06/08 13:33
#include<iostream>
#include<string>
using namespace std;


template <typename Any>
Any maxn(const Any *p, int n);


template<> string maxn<string>(const string *p, int n);


int main()
{
int arr[7] = { 1,5,24,423,123,14,53 };
double dor[4] = { 3245.325,23523.423,1245.435,1245 };
string str[6] = { "fajsfhsdgh","fakjsgjhfdhur","absdfhbdsguer","fsahgjdsiairng","fasjgjsirhb","fakjsghur" };
int max = 0;
double maxi = 0.0;
string maxchar;
max = maxn(arr, 7);
maxi = maxn(dor, 4);
maxchar = maxn(str, 6);
cout << max << endl;
cout.setf(ios_base::fixed);
cout.precision(5);
cout << maxi << endl;
cout << maxchar << endl;
cin.get();
cin.get();
return 0;
}
template <typename Any>
Any maxn(const Any *p, int n)
{
Any max = p[0];
for (int i = 1;i < n;i++)
{
if (p[i] > max)
max = p[i];
}
return max;
}


template<> string maxn<string>(const string *p, int n)
{
string pf;
int max = 0;
for (int i = 0;i < n;i++)
if (p[i].size() > max)
{
max = p[i].size();
pf = p[i];
}
return pf;
}
0 0