std::forwar...insert_after(3)
β¨ std::forward_list::insert_after β¨
okay bestie so listen β this is a page from the C++ Standard Library manual (section 3, mwah), and it's all about std::forward_list::insert_after. Like the name literally says: you insert stuff after a position. No insert_before girlies here, forward_list only goes one way and we respect it π
πΈ SYNOPSIS πΈ
iterator insert_after( const_iterator pos, const T& value );
(1) (since C++11)
iterator insert_after( const_iterator pos, T&& value );
(2) (since C++11)
iterator insert_after( const_iterator pos, size_type count, const T& value );
(3) (since C++11)
template< class InputIt >
iterator insert_after( const_iterator pos, InputIt first, InputIt last );
(4) (since C++11)
iterator insert_after( const_iterator pos, std::initializer_list<T> ilist );
(5) (since C++11)
π¦ DESCRIPTION π¦
The whole vibe: inserts elements after the specified position in the container. That's it, that's the job, and she ate π₯
1,2)β insertsvalueafter the element pointed to bypos.3)β insertscountcopies of thevalueafter the element pointed to bypos.4)β inserts elements from the range[first, last)after the element pointed to bypos. β οΈ The behavior is undefined iffirstandlastare iterators into*this. Like fr fr do NOT do that, that's undefined behavior territory and we don't go there π5)β inserts elements from initializer listilist.
No iterators or references are invalidated. Not a single one. she's careful like that π
π 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
β TYPE REQUIREMENTS β
InputItmust meet the requirements ofLegacyInputIterator. (iykyk π)
π RETURN VALUE π
1,2)β iterator to the inserted element.3)β iterator to the last element inserted, orposifcount == 0.4)β iterator to the last element inserted, orposiffirst == last.5)β iterator to the last element inserted, orposifilistis empty.
like she always gives you something back, even when nothing got inserted. main character energy π
π§ EXCEPTIONS π§
If an exception is thrown for any reason, these functions have no effect β that's the strong exception safety guarantee. lowkey iconic, she cleans up after herself β¨
π COMPLEXITY π
1,2)β Constant.3)β Linear incount.4)β Linear instd::distance(first, last).5)β Linear inilist.size().
π EXAMPLE π
// Run this code
#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);
}
π«Ά Output π«Ά
list: {1, 2, 3, 4, 5}
list: {1, -6, 2, 3, 4, 5}
list: {1, -6, -7, -7, 2, 3, 4, 5}
list: {1, -6, -7, -7, -8, -9, -10, 2, 3, 4, 5}
list: {1, -6, -7, -7, -8, -9, -10, -11, -12, -13, -14, 2, 3, 4, 5}
look at all those negative numbers sliding in. obsessed ππ₯
π SEE ALSO π
emplace_afterβ constructs elements in-place after an element (public member function)push_frontβ inserts an element to the beginning (public member function)
okay that's the whole page bestie, go forth and insert after π
source/version:http://cppreference.com, dated2024.06.10β and this beauty isstd::forwar...insert_after(3)(yes the title got cut off in the manual header, we don't know her full name either, it's giving mysterious πΈ). mwah, bye! π β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms