Template function C++ utility standard header essence
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; } }
The comparison of if (&_Left != &_Right) is very essential. It can speed up the efficiency without swapping at the same time, but if it needs to be swapped, it will be compared once more;
Thoughts can be learned. .