64,637
社区成员
发帖
与我相关
我的任务
分享
template<class _Ty> inline
void swap(_Ty& _Left, _Ty& _Right)
{ // exchange values stored at _Left and _Right
if (&_Left != &_Right)
{ // different, worth swapping
_Ty _Tmp = _Left;
_Left = _Right;
_Right = _Tmp;
}
}
template<class T>
void swap2(T& _Left, T& _Right)
{ // exchange values stored at _Left and _Right
if (&_Left != &_Right)
{ // different, worth swapping
T _Tmp = _Left;
_Left = _Right;
_Right = _Tmp;
}
}
int main()
{
int a=10,b=20;
int *p=&a,*q=&b;
swap2(p,q);
cout<<a<<"\t"<<b<<endl;
return 0;
}