"Atlas" <Ma*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
: John Harrison wrote:
....
: > >>Atlas wrote:
: > >>
: > >>>To swap to nodes in list, changing pointers is enough. But the stl
swap
: > >>>method seems to copy their values, doesn't it?
....
: > There are three different versions of splice. Two of them allow a list
: > to splice with itself. It all depends on precisely what the OP wants
to do.
: >
: > john
:
: Unfortunately I didn't find any one like "Two of them allow a list to
: splice with itself".
I'm surprised this thread does not seem to get anywhere yet: this problem
ought to have a reasonably simple solution.
So let me concretely give it a try.
The easiest seems to use the second form of list.splice().
From the standard:
<<<
void splice(iterator position, list<T,Allocator>& x, iterator i);
Effects: Inserts an element pointed to by i from list x before position
and removes the element from x.
The result is unchanged if position == i or position == ++i. Invalidates
only the iterators and references to the spliced element.
Throws: Nothing
Requires: i is a valid dereferenceable iterator of x.
Complexity: Constant time.
Since you can have position==i, it seems obvious that x can be the same
list as 'this'.
So wouldn't something like the following work?
template<class T, class Allocator>
void swap_list_items
( std::list<T,Allocator>& l
, std::list<T,Allocator>::iterator a
, std::list<T,Allocator>::iterator b
)
{
std::list<T,Allocator>::iterator aPlus = a;
++aPlus; // after position of a, will not be invalidated
l.splice( b, l, a ); // move a before b, invalidates a
l.splice( aPlus, l, b ); // move b before aPlus (where A was)
}
This untested snippet is just a proposed solution, for others to review,
and might be buggy. But I'm sure we can work something out ;)
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form