My assumption is that in a std::list<> the swap function for the list itself is done by swapping the anchor node. The node can access the previous node and update the previous node’s next pointer easily to point to the anchor of the other list; but this cannot be done in std::forward_list (well, it can be, it is just very costly).
If my assumption is correct, how is swap() implemented in std::forward_list in an efficient manner? And while we are at it, how is swap() implemented for the iterators of std::forward_list?
A
std::forward_listis simply a singly-linked rather than doubly-linked list likestd::list, therefore you can simply swap theheadandtailpointers of the list to accomplish aswap()operation.