分类: 数据结构

1 篇文章

函数模版和类模版简单例子
#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 = " << opDoubl…