3 ms·
Imagine reversing a string: while(pStart <= pEnd) swap(*pStart++, *pEnd--); If swap doesn't work for a self swap, this code would break in the case o
by dkopi 10y ago
Imagine reversing a string:
while(pStart <= pEnd)
swap(*pStart++, *pEnd--);
If swap doesn't work for a self swap, this code would break in the case of an odd length string.
It's an easy fix in this case, but being able to swap an element with itself is convenient.
- Matheus28 10y agoA more common case, would be swapping a vector element with the back of the vector and popping it off, which is kinda common.
- plorkyeran 10y agoIt was common pre-C++11, but these days you should just be doing a move and pop rather than a swap and pop.
- adrianN 10y agoLuckily all existing code has been modernized to C++11 /s.