std::forward_list<T,Allocator>::insert_range_after

From cppreference.com
 
 
 
 
template< container-compatible-range<T> R >
iterator insert_range_after( const_iterator pos, R&& rg );
(since C++23)

Inserts, in non-reversing order, the copies of elements in rg before pos. Each iterator in the range rg is dereferenced exactly once.

pos must be any dereferenceable iterator in the range [begin()end()) or the before_begin() iterator (thus, end() is not a valid argument for pos).

No iterators or references become invalidated.

The behavior is undefined if rg overlaps with the container.

Parameters

pos - an iterator after which the content will be inserted.
rg - a container compatible range, that is, an input_range whose elements are convertible to T.
Type requirements
-
T must be EmplaceConstructible into forward_list from *ranges::begin(rg). Otherwise, the behavior is undefined.

Return value

An iterator that points at the copy of the last element inserted into forward_list or at pos if rg is empty.

Complexity

Linear in size of rg.

Notes

Feature-test macro Value Std Comment
__cpp_lib_containers_ranges 202202L (C++23) Ranges-aware construction and insertion

Example

#include <algorithm>
#include <cassert>
#include <forward_list>
#include <iterator>
 
int main()
{
    auto data = std::forward_list{1, 2, 3, 4};
    auto pos = std::next(data.cbegin());
    assert(*pos == 2);
    const auto infix = {-1, -2, -3};
 
#ifdef __cpp_lib_containers_ranges
    data.insert_range_after(pos, infix);
#else
    data.insert_after(pos, infix.begin(), infix.end());
#endif
 
    assert(std::ranges::equal(data, std::forward_list{1, 2, -1, -2, -3, 3, 4}));
}

See also

adds a range of elements to the beginning
(public member function)
inserts elements after an element
(public member function)
constructs elements in-place after an element
(public member function)