1. 程式人生 > >編寫一求兩個數的最大值的函式Max, 要求用模板實現對任意資料型別資料都可應用該函式求取結果,

編寫一求兩個數的最大值的函式Max, 要求用模板實現對任意資料型別資料都可應用該函式求取結果,

 /*編寫一求兩個數的最大值的函式Max,
 要求用模板實現對任意資料型別資料都可應用該函式求取結果,
 在main()函式中分別用整型、實型、字元型資料進行測試。 */
#include<iostream>
#include<string.h>
using namespace std;

template <class T>
T  max(T & x,T & y){
    if(x<y){
         return y;
    }
    else {
        return x;
    }
}
int main()
{
   cout<<max(2 ,3) << endl;
   cout<<max(2.0 ,3.1) << endl;
   cout<<max('a','b') << endl;
    return 0;
}