本文最后更新于 342 天前,其中的信息可能已经有所发展或是发生改变。
内容目录
#include <iostream>
using namespace std;
// 函数模版
template <typename T>
void Swap(T& a, T& b)
{
T t = a;
a = b;
b = t;
}
// 类模版
template <typename T>
class Op
{
public:
T process(T v)
{
return v * v;
}
};
int main()
{
int a = 2;
int b = 1;
Swap(a, b);
cout << "a = " << a << " " << "b = " << b << endl;
double c = 0.01;
double d = 0.02;
Swap<double>(d, c);
cout << "c = " << c << " " << "d = " << d << endl;
Op<int> opInt; // 需要指明类型
Op<double> opDouble;
cout << "5 * 5 = " << opInt.process(5) << endl;
cout << "0.3 * 0.3 = " << opDouble.process(0.3) << endl;
return 0;
}
👍