std::forward_list<T,Allocator>::insert_after
Min standard notice:
Inserts elements after the specified position in the container.
# Declarations
iterator insert_after( const_iterator pos, const T& value );
(since C++11)
iterator insert_after( const_iterator pos, T&& value );
(since C++11)
iterator insert_after( const_iterator pos, size_type count, const T& value );
(since C++11)
template< class InputIt >
iterator insert_after( const_iterator pos, InputIt first, InputIt last );
(since C++11)
iterator insert_after( const_iterator pos, std::initializer_list<T> ilist );
(since C++11)
# Parameters
pos: iterator after which the content will be insertedvalue: element value to insertcount: number of copies to insertfirst, last: the range of elements to insertilist: initializer list to insert the values from
# Example
#include <forward_list>
#include <iostream>
#include <string>
#include <vector>
void print(const std::forward_list<int>& list)
{
std::cout << "list: {";
for (char comma[3] = {'\0', ' ', '\0'}; int i : list)
{
std::cout << comma << i;
comma[0] = ',';
}
std::cout << "}\n";
}
int main()
{
std::forward_list<int> ints{1, 2, 3, 4, 5};
print(ints);
// insert_after (2)
auto beginIt = ints.begin();
ints.insert_after(beginIt, -6);
print(ints);
// insert_after (3)
auto anotherIt = beginIt;
++anotherIt;
anotherIt = ints.insert_after(anotherIt, 2, -7);
print(ints);
// insert_after (4)
const std::vector<int> v = {-8, -9, -10};
anotherIt = ints.insert_after(anotherIt, v.cbegin(), v.cend());
print(ints);
// insert_after (5)
ints.insert_after(anotherIt, {-11, -12, -13, -14});
print(ints);
}