0
点赞
收藏
分享

微信扫一扫

用模板比较不同类型值,求出最大的那个


//main.cpp
#include <iostream>
#include "maximum.h"

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
//int类型
int int1,int2,int3;
cout<<"input 3 integer:"<<endl;
cin>>int1>>int2>>int3;
cout<<"maximum integer value is "<<maximum(int1,int2,int3)<<endl;

//double类型
double double1,double2,double3;
cout<<"input 3 double type number:"<<endl;
cin>>double1>>double2>>double3;
cout<<"maximum double type value is "<<maximum(double1,double2,double3);

return 0;
}

//maximum.h
//求最大值的方法
template<class T>
T maximum(T value1,T value2,T value3)
{
T maximumValue=value1>value2?value1:value2;
maximumValue=maxim
umValue>value3?maximumValue:value3;

return maximumValue;
}

效果如下

用模板比较不同类型值,求出最大的那个_ios


举报

相关推荐

0 条评论