Functioning splice () in cpp

How does splicing work? I read about it at http://www.cplusplus.com/reference/list/list/splice/

I could not understand this part from the code in the link above:

mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end()); 
+4
source share
5 answers

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

 [1, 2, 3, 4, 5] 

Now you create an iterator in a list called it and you promote it in 3 positions:

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

then you splic the list into yourself, to the beginning of the list (first parameter) in the same list (second parameter), from the position indicated by it (third parameter) to the end (fourth parameter), which gives the following result:

 [4, 5, 1, 2, 3] 

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

+5
source

The reason you need to provide a list source is because otherwise the elements cannot be removed from it.

+3
source

The fourth parameter of the splicing function moves (does not copy) your range to your position indicated by the 1st parameter.

In your example, you move the elements of your list to another position in the list (more precisely, the end of the list to the beginning).

+1
source
 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's pretty hard to see by looking at it, because we have 2 lists and 2 iterators.

The word position gives it away. It indicates where to execute insert .

The iterator that is i is what moves. In the second overload, the range moves from start to finish , but not finish . finish may be the end of the list.

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

Please note that cplusplus.com claims that iterators become invalid after splicing, but in fact this is not so, they remain valid.

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

In your example:

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

it should be an iterator in mylist1. It looks like this should not be mylist1.begin() .

Your operation moves all items from it forward to the beginning of the list.

+1
source

The C ++ function std :: list :: splice () transfers elements in the range from the first to the last from x to * using move semantics. Elements are inserted before the element indicated by the position. The syntax will be like this

 void splice (const_iterator position, list&& x, const_iterator first, const_iterator lets take an example of splice list<int> l1 = {1, 2}; list<int> l2 = {3, 4, 5}; l1.splice(l1.end(), move(l2), l2.begin(), l2.end()); cout << "Contents of list l1 after splice operation" << endl; for (list<int>iterator::it = l1.begin(); it != l1.end(); ++it) cout << *it << endl; 

Output

List contents l1 after splicing operation 1 2 3 4 5

0
source

All Articles