6

How does splice works? I read about it in http://www.cplusplus.com/reference/list/list/splice/

I couldn't understand this part from the code in the above link:

mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
scottt
  • 7,008
  • 27
  • 37
Saikiran
  • 143
  • 2
  • 7
  • Not exactly a duplicate but see this question: http://stackoverflow.com/questions/4912380/move-list-element-to-the-end-in-stl/4912492#4912492 – CashCow Dec 12 '12 at 10:17

4 Answers4

5

Imagine you have a list of integers with the following contents:

[1, 2, 3, 4, 5]

Now you create an iterator in the list called it and you advance it of 3 positions:

[1, 2, 3, 4, 5]
          ^
          'it' points here

then you splice the list into itself, to the list's beginning (first parameter), in the same list (second parameter), from the position pointed by it (third parameter), to the end (fourth parameter), which gives the following result:

[4, 5, 1, 2, 3]

So you effectively rotated the list of two elements to the right.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
SirDarius
  • 41,440
  • 8
  • 86
  • 100
  • No, my question to be exact was, what is the function of 'mylist1,it' in here... mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end()); – Saikiran Dec 12 '12 at 10:09
  • I have answered that, mylist1 (the second parameter) must be an element of the same type as the mylist1 object, therefore mylist1 is the best candidate, and `it` is an iterator in the same list – SirDarius Dec 12 '12 at 10:11
  • 1
    I think the question is why is the second argument (`x`) needed for the second and third versions of the splice functions. – Andreas Brinck Dec 12 '12 at 10:16
  • And can you please tell me how did the element 2 got deleted by the code... mylist2.splice (mylist2.begin(),mylist1, it); – Saikiran Dec 12 '12 at 10:18
  • @AndreasBrinck good point, but the documentation doesn't really explain that, we need to look at some source code – SirDarius Dec 12 '12 at 10:18
  • @Andreas Brinck was spot on to understand my question. My question was exactly the same. – Saikiran Dec 12 '12 at 10:22
  • @Sakiran Just realized why (doh), see my answer. – Andreas Brinck Dec 12 '12 at 10:25
4

The reason you need to supply the source list is that otherwise the elements can't be removed from it.

Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114
  • Can you tell me why this code works as well. I made a list and passed it as the second argument and it works. ` mylist1.splice ( mylist1.begin(), mylist2, it, mylist1.end()); ` – Saurabh Rana Mar 16 '19 at 04:13
1

The fourth parameter of the splice function moves (not copy) your range to your position specified by the 1st parameter.

In your example you're moving elements of your list to another position in the list (more precisely the end of the list to the beginning).

Uflex
  • 1,396
  • 1
  • 13
  • 32
1
void list<T,Allocator>::splice ( iterator position, list<T,Allocator>& x, iterator i );
 
void list<T,Allocator>::splice ( iterator position, list<T,Allocator>& x, iterator start, iterator finish );

It is quite difficult to see just by looking at it, because we have 2 lists and 2 iterators.

The word position gives it away though. It is stating where to perform the insert.

The iterator which is i is what gets moved. In the second overload, the range from start to finish but not finish itself are moved. finish may be the end of the list.

The position must belong to the this list. The iterator must belong to the x list. Elements are inserted just before position in the source (this) list and are at the same time removed from the x list.

Note that cplusplus.com states that the iterators become invalidated once spliced however this is not actually the case, they do remain valid.

cplusplus.com is correct in that the position may not be one of the spliced elements (in the case that the lists are the same)

In your example:

mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());

it must be an iterator within mylist1. It would appear it must not be mylist1.begin().

Your operation moves all the elements from it onward to the beginning of the list.

CashCow
  • 30,981
  • 5
  • 61
  • 92