#include <iostream>
using namespace std;
template<typename T>    
T abs(T x)
{
    return x<0?-x:x;        //运算
}
int main()
{
    int iN=-5;
    double dN=-5.5;
    cout<<abs(iN)<<endl;    //输出
    cout<<abs(dN)<<endl;    //输出

    getchar();
    return 0;

}

代码如上,

结果:

简单模版函数

分析:

typename或class标识符,指明可以接收一个类型参数。可以是内部类型或是自定义类型。

相关文章:

  • 2021-05-22
  • 2022-12-23
  • 2022-01-05
  • 2022-02-04
  • 2022-12-23
  • 2021-09-02
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2022-02-01
  • 2022-01-20
  • 2021-04-05
  • 2021-12-18
相关资源
相似解决方案