public member function
<forward_list>

std::forward_list::insert_after

(1)
iterator insert_after ( const_iterator position, const value_type& val );
(2)
iterator insert_after ( const_iterator position, value_type&& val );
(3)
iterator insert_after ( const_iterator position, size_type n, const value_type& val );
(4)
template <class InputIterator>  iterator insert_after ( const_iterator position, InputIterator first, InputIterator last );
(5)
iterator insert_after ( const_iterator position, initializer_list<value_type> il );
Insert elements
The container is extended by inserting new elements after the element at position.

This effectively increases the container size by the amount of elements inserted.

Unlike other standard sequence containers, list and forward_list objects are specifically designed to be efficient inserting and removing elements in any position, even in the middle of the sequence.

A similar member function exists, emplace_after, which constructs an inserted element object directly in place, without performing any copy or move operation.

The arguments determine how many elements are inserted and to which values they are initialized:

Parameters

position
Position in the container after which the new elements are inserted.
Member type const_iterator is a forward iterator type that points to elements.
val
Value to be copied to (or moved as) the inserted elements.
Member type value_type is the type of the elements in the container, defined in forward_list as an alias of its first template parameter (T).
n
Number of elements to insert. Each element is initialized to a copy of val.
Member type size_type is an unsigned integral type.
first, last
Iterators specifying a range of elements. Copies of the elements in the range [first,last) are inserted at position (in the same order).
Notice that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
The function template argument InputIterator shall be an input iterator type that points to elements of a type from which value_type objects can be constructed.
il
An initializer_list object. Copies of these elements are inserted at position (in the same order).
These objects are automatically constructed from initializer list declarators.
Member type value_type is the type of the elements in the container, defined in forward_list as an alias of its first template parameter (T).

Return value

An iterator that points to the last of the newly inserted elements, or position if no element was inserted.

Member type iterator is a forward iterator type that points to elements.

The storage for the new element is allocated using allocator_traits<allocator_type>::construct(), which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).

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
// forward_list::insert_after
#include <iostream>
#include <array>
#include <forward_list>

int main ()
{
  std::array<int,3> myarray = { 11, 22, 33 };
  std::forward_list<int> mylist;
  std::forward_list<int>::iterator it;

  it = mylist.insert_after ( mylist.before_begin(), 10 );          // 10
                                                                   //  ^  <- it
  it = mylist.insert_after ( it, 2, 20 );                          // 10 20 20
                                                                   //        ^
  it = mylist.insert_after ( it, myarray.begin(), myarray.end() ); // 10 20 20 11 22 33
                                                                   //                 ^
  it = mylist.begin();                                             //  ^
  it = mylist.insert_after ( it, {1,2,3} );                        // 10 1 2 3 20 20 11 22 33
                                                                   //        ^

  std::cout << "mylist contains:";
  for (int& x: mylist) std::cout << ' ' << x;
  std::cout << '\n';
  return 0;
}
Output:
mylist contains: 10 1 2 3 20 20 11 22 33


Complexity

Linear on the number of elements inserted (copy/move construction).

Iterator validity

No changes.

Data races

The container is modified.
No contained elements are accessed. Concurrently accessing or modifying different elements is safe, although iterating ranges that include position is not.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the container.
If allocator_traits::construct is not supported with the appropriate arguments for the element constructions, or if an invalid position or range is specified, it causes undefined behavior.

See also