Exchange function swap learning

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. .


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325809352&siteId=291194637