swapshould be used in preference toiter_swap, which was included in the C++ Standard for backward compatibility.
But comp.std.c++ says:
Most STL algorithms operate on iterator ranges. It therefore makes sense to
useiter_swapwhen swapping elements within those ranges, since that is its
intended purpose — swapping the elements pointed to by two iterators. This
allows optimizations for node-based sequences such asstd::list, whereby the
nodes are just relinked, rather than the data actually being swapped.
So which one is correct? Should I use iter_swap, or should I use swap? (Is iter_swap only for backwards compatibility?) Why?
The standard itself has very few mentions of
iter_swap:swap(*a, *b), although there is no stipulation that it must be implemented that way.*aand*bmust be “swappable”, which implies thatswap(*a, *b)must be valid, and thus the dereferenced types must be identical, although the iterator types do not have to be.iter_swapis required to be used in the implementation ofstd::reverse. No such requirement is placed on any other algorithm, so this seems to be an oddity.To borrow what sehe had found from the SGI docs:
All of these seem to suggest that it is an artifact of the past.