简单函数template max

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

template<typename elemtypea>
elemtypea mymax(const vector<elemtypea> &a)
{
       return  *max_element( a.begin(), a.end() );
}

template<typename elemtypea >
elemtypea mymax(const elemtypea *a, int b)
{
    int pointer=0;
    for (int i=0;i<b;i++)
    {
        if( a[pointer]<a[i])
            pointer=i;
    }
    return a[pointer];
}

template <typename elemtypea>
elemtypea mymax( elemtypea  a,  elemtypea b)
{
        return a>b?a:b;
 }
 
int main()
{ 
    int a=1,b=2;
    float c=1.0,d=2.0;
    string e="abc";
    int f[3]={1,2,3};
    float g[3]={1.34,4.4,23.3};
    vector<string> h;
    h.push_back("da");h.push_back("df");
    char aa[]="dfsaf";
    char bb[]="dsaf";
    cout<<mymax(a,b)<<endl;
    cout<<mymax(c,d)<<endl;
    cout<<mymax(f,3)<<endl;
    cout<<mymax(g,3)<<endl;
    cout<<mymax(h)<<endl;
    cout<<mymax(aa,bb)<<endl;
}
原文地址:https://www.cnblogs.com/huashiyiqike/p/3859098.html