, T1 T2 . Pair<T1,T2> - , Pair<T2,T1>. , , .
:
template< class T1, class T2 >
Pair<T2,T1> swap(const Pair<T1,T2>& pair) {
return Pair<T2,T1>(pair.second, pair.first);
}
( , Pair.)
, T1 T2 :
template< class T >
Pair<T,T>& swap(Pair<T,T>& pair) {
using std::swap;
swap(pair.first, pair.second);
return pair;
}
, , .
- Pair, :
template< class T1, class T2 >
class Pair {
T1 first;
T2 second;
template< class A1, class A2 >
Pair(const A1& a1, const A2& a2) : first(a1), second (a2) {}
};
:
Pair<int,double> p1(42,47.11);
Pair<double,int> p2(p1.second,p1.first);
, , , :
Pair<char,float> p3(p1.second, p1.first);