public member function

std::forward_list::splice_after

<forward_list>
(1)
void splice_after ( const_iterator position, forward_list& fwdlst );
void splice_after ( const_iterator position, forward_list&& fwdlst );
(2)
void splice_after ( const_iterator position, forward_list& fwdlst, const_iterator i );
void splice_after ( const_iterator position, forward_list&& fwdlst, const_iterator i );
(3)
void splice_after ( const_iterator position, forward_list& fwdlst,
                    const_iterator first, const_iterator last );
void splice_after ( const_iterator position, forward_list&& fwdlst,
                    const_iterator first, const_iterator last );
Move elements from another forward_list
Moves elements from fwdlst into the container inserting them after the element pointed by position, effectively inserting those elements into the container and removing them from fwdlst.

This increases the container size by the amount of elements inserted, and reduces the size of fwdlst by the same amount ( whenever fwdlst is not the same as *this ).

The operation does not involve the construction, destruction or copy of any element object. They are moved, no matter which version is used.

The iterators that pointed to moved elements keep pointing to the same elements, but now behave as iterators into the container they have been transferred to.

Parameters

position
Position within the container after which the elements of fwdlst are inserted.
Member type const_iterator is a forward itertor.
fwdlst
A forward_list object of the same type (i.e., with the same template parameters).
This parameter may be *this if position points to an element not actually being spliced: for the two-parameters version (1), this is never the case, but for the other versions this is possible.
Note that this function modifies fwdlst, removing elements from it, no matter whether an lvalue or rvalue reference is passed.
i
Iterator to an element in fwdlst preceding the one to be moved. Only the single element that follows this is moved.
Member type const_iterator is a forward itertor.
first,last
Iterators specifying an open interval range of elements in fwdlst. The function moves the elements in the range (first,last) to position.
Notice that, in this function, the range is open and includes all the elements between first and last, but not first nor last themselves.
Member type const_iterator is a forward itertor.

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// forward_list::splice_after
#include <iostream>
#include <forward_list>

int main ()
{
  std::forward_list<int> first = { 1, 2, 3 };
  std::forward_list<int> second = { 10, 20, 30  };

  auto it = first.begin();  // points to the 1

  first.splice_after ( first.before_begin(), second );
                          // first: 10 20 30 1 2 3
                          // second: (empty)
                          // "it" still points to the 1 (now first's 4th element)

  second.splice_after ( second.before_begin(), first, first.begin(), it);
                          // first: 10 2 3
                          // second: 20 30 1

  first.splice_after ( first.before_begin(), second, second.begin() );
                          // first: 30 10 2 3
                          // second: 20 1
                          // * notice that what is moved is AFTER the iterator

  std::cout << "first contains:";
  for (int& x: first) std::cout << " " << x;
  std::cout << std::endl;

  std::cout << "second contains:";
  for (int& x: second) std::cout << " " << x;
  std::cout << std::endl;

  return 0;
}

Output:
first contains: 30 10 2 3
second contains: 20 1

Complexity

Linear in the number of elements moved (iterator advance).

Iterator validity

The iterators, pointers and references that referred to moved elements keep referring to those same elements, but iterators now iterate into the container the elements have been transferred to.

See also