public member function
<forward_list>

std::forward_list::splice_after

entire list (1)
void splice_after (const_iterator position, forward_list& fwdlst);void splice_after (const_iterator position, forward_list&& fwdlst);
single element (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);
element range (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);
Transfer elements from another forward_list
Transfers elements from fwdlst into the container inserting them after the element pointed by position.

This effectively inserts those elements into the container and removes them from fwdlst, altering the sizes of both containers. The operation does not involve the construction or destruction of any element. They are transferred, no matter whether fwdlst is an lvalue or an rvalue, or whether thevalue_type是否支持移动构造。

The first version (1) transfers all the elements of fwdlst into the container.
The second version (2) transfers only the element pointed by i from fwdlst into the container.
The third version (3) transfers the range(first,last)from fwdlst into the container.

参数

position
Position within the container after which the elements of fwdlst are inserted.
成员类型const_iteratoris a forward iterator that points to const elements.
fwdlst
A forward_list object of the same type (i.e., with the same template parameters,TAlloc) and using an identical allocator.
This parameter may be*thisif 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 transferred. Only the single element that follows this is transferred.
成员类型const_iteratoris a forward iterator that points to const elements.
first,last
Iterators specifying an open interval range of elements in fwdlst. The function transfers 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.
成员类型const_iteratoris a forward iterator that points to const elements.

返回值



示例

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 1 2 3
                          // second: 20 30

  first.splice_after ( first.before_begin(), second, second.begin() );
                          // first: 30 10 1 2 3
                          // second: 20
                          // * 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;
}
输出
first contains: 30 10 1 2 3
second contains: 20


复杂度

Up to linear in the number of elements transferred.

迭代器有效性

调用前,容器的迭代器、指针和引用没有变化。
先前指向被转移元素的迭代器、指针和引用将继续指向这些相同的元素,但迭代器现在将指向已转移到容器中的元素。

数据竞争

Both the container and fwdlst are modified.
Concurrently accessing or modifying their elements is safe, although iterating x or ranges that include position is not.

异常安全

If the allocators in both containers do not compare equal, if any of the iterators or ranges specified is not valid, or if x is*thisin (1), or if position is in the range[first,last)in (3), it causes undefined behavior.
否则,该函数永远不会抛出异常(无抛出保证)。

另见