std::forwar..._range_after(3)
πΈ std::forward_list::insert_range_after πΈ
okay bestie, this is the std::forwar..._range_after(3) page from the C++ Standard Libary, and it's all about std::forward_list::insert_range_after. It's giving "new range-aware member function just dropped" energy, and honestly? We're obsessed. π
β¨ NAME β¨
std::forward_list::insert_range_after β std::forward_list::insert_range_after
π«Ά Synopsis π«Ά
Since C++23, no cap. Signature:
template< container-compatible-range<T> R >
iterator insert_range_after( const_iterator pos, R&& rg );
It inserts, in non-reversing order, the copies of elements in rg before pos. Each iterator in the range rg is dereferenced exactly once. Literally 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. That's the good news, bestie. π«Ά
The behavior is undefined if rg overlaps with the container. Like, do not do that. π
π Parameters π
posβ an iterator after which the content will be inserted.rgβ a container compatible range, that is, aninput_rangewhose elements are convertible toT.
πΈ Type requirements πΈ
Tmust beEmplaceConstructibleintoforward_listfrom*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. Fr fr.
π¦ Notes π¦
Feature-test macro vibe check:
- Feature-test macro:
__cpp_lib_containers_ranges; Value:202202L; Std:(C++23); Feature: Ranges-aware construction and insertion.
π Example π
// Run this code
#include <algorithm>
#include <cassert>
#include <forward_list>
#include <iterator>
#include <vector>
int main()
{
auto container = std::forward_list{1, 2, 3, 4};
auto pos = std::next(container.cbegin());
assert(*pos == 2);
const auto rg = std::vector{-1, -2, -3};
#ifdef __cpp_lib_containers_ranges
container.insert_range_after(pos, rg);
#else
container.insert_after(pos, rg.cbegin(), rg.cend());
#endif
assert(std::ranges::equal(container, std::vector{1, 2, -1, -2, -3,
3, 4}));
}
π See also π
prepend_rangeβ adds a range of elements to the beginning (C++23) (public member function).insert_afterβ inserts elements after an element (public member function).emplace_afterβ constructs elements in-place after an element (public member function).
source/version/date moment: this page lives at http://cppreference.com, dated 2024.06.10, and it's the std::forwar..._range_after(3) page. Bye bestie, go insert a range after something β or before pos, according to the lore β and slay πβ¨
π¦ 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