#include<iostream>
#include<cstdio>
#include<string>
#include<ostream>
#include<istream>
using namespace std;
template <class T1,class T2>
class Pair{
public:
T1 key;
T2 value;
Pair(T1 k,T2 v):key(k), value(v){}
bool operator <(const Pair<T1, T2> & p)const;
friend ostream & operator << <>(ostream & os, const Pair<T1, T2> & p);
friend istream & operator >> <>(istream & is, Pair<T1, T2> & p);
};
template<class T1,class T2>
bool Pair<T1, T2>::operator <(const Pair<T1,T2> & p)const
{
return key < p.key;
}
template <class T1,class T2>
ostream & operator << <>(ostream & os, const Pair<T1, T2> & p)
{
os << p.key << " " << p.value << endl;
return os;
}
template<class T1,class T2>
istream & operator >> <>(istream & is,Pair<T1, T2> & p)
{
is >> p.key >> p.value;
return is;
}
int main()
{
Pair<string, int> student("Tom", 19);
cout << student;
cin >> student;
cout << student;
system("pause");
return 0;
}
类模板的原理
最新推荐文章于 2023-06-25 11:07:01 发布