#include
using namespace std;
template
class Pair{
public:
T1 key;
T2 value;
Pair(T1 k, T2 v): key(k), value(v){};
bool operator < (const Pair & p)const;
};
template
bool Pair::operator < (const Pair & p) const{
return key < p.key;
}
int main()
{
Pair student("Tom", 19);
// 编译器由类模板生成类的过程叫类模板的实例化
// -编译器自动用具体的数据类型
// -替换类模板中的类型参数, 生成模板类的代码
// 由类模板实例化得到的类叫模板类
// -为类型参数指定的数据类型不同, 得到的模板类不同
Pair score("Tim", 98.5);
类模版
发表评论