利用函数模板解决双倍功能

代码如下:

#include <iostream>
using namespace std;

template <class T>
T Double(T x){return 2*x;}

int main(void){
    char c='\0';
    int i=0;
    long l=0;
    scanf("%c%d%ld",&c,&i,&l);
    cout<<Double(c)<<endl;
    cout<<Double(i)<<endl;
    cout<<Double(l)<<endl;
    float f=1.1;
    double d=2.2;
    scanf("%f%lf",&f,&d);
    cout<<Double(f)<<endl;
    cout<<Double(d)<<endl;
    return 0;
}

运行截图如下:

猜你喜欢

转载自blog.csdn.net/y0205yang/article/details/118653157