public member function

std::forward_list::insert_after

<forward_list>
(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.
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 position.
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 template type shall be an input iterator type.
il
An initializer_list object. The compiler will automatically construct such objects 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.

Member type iterator is a forward iterator type.

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 << std::endl;
  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

All of the iterators, pointers and references remain valid after the insertion and refer to the same elements they were referring to before.

See also