Difference between revisions of "cpp/container/forward list/insert range after"
From cppreference.com
< cpp | container | forward list
(+page, +example; this function is unique to `forward_list` ⇒ not-templated.) |
Andreas Krug (Talk | contribs) m (.) |
||
(2 intermediate revisions by one user not shown) | |||
Line 9: | Line 9: | ||
Inserts, in non-reversing order, the copies of elements in {{c|rg}} before {{c|pos}}. Each iterator in the range {{c|rg}} is dereferenced exactly once. | Inserts, in non-reversing order, the copies of elements in {{c|rg}} before {{c|pos}}. Each iterator in the range {{c|rg}} is dereferenced exactly once. | ||
− | {{c|pos}} must be any dereferenceable iterator in the range {{range/core|{{ | + | {{c|pos}} must be any dereferenceable iterator in the range {{range/core|{{rlpf|begin}}|{{rlpf|end}}}} or the {{rlpf|before_begin}} iterator (thus, {{rlpf|end}} is not a valid argument for {{c|pos}}). |
No iterators or references become invalidated. | No iterators or references become invalidated. | ||
Line 17: | Line 17: | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par|pos|an iterator after which the content will be inserted | + | {{par|pos|an iterator after which the content will be inserted}} |
− | {{par|rg|a {{ls|cpp/ranges/to | + | {{par|rg|a {{ls|cpp/ranges/to#container compatible range}}, that is, an {{lconcept|input_range}} whose elements are convertible to {{tt|T}}}} |
{{par hreq}} | {{par hreq}} | ||
{{par req|{{tt|T}} must be {{named req|EmplaceConstructible}} into {{tt|forward_list}} from {{c|*ranges::begin(rg)}}. Otherwise, the behavior is undefined.}} | {{par req|{{tt|T}} must be {{named req|EmplaceConstructible}} into {{tt|forward_list}} from {{c|*ranges::begin(rg)}}. Otherwise, the behavior is undefined.}} | ||
Line 33: | Line 33: | ||
===Example=== | ===Example=== | ||
− | {{example|code= | + | {{example |
+ | |code= | ||
#include <algorithm> | #include <algorithm> | ||
#include <cassert> | #include <cassert> | ||
#include <forward_list> | #include <forward_list> | ||
#include <iterator> | #include <iterator> | ||
+ | #include <vector> | ||
int main() | int main() | ||
{ | { | ||
− | auto | + | auto container = std::forward_list{1, 2, 3, 4}; |
− | auto pos = std::next( | + | auto pos = std::next(container.cbegin()); |
assert(*pos == 2); | assert(*pos == 2); | ||
− | const auto | + | const auto rg = std::vector{-1, -2, -3}; |
#ifdef __cpp_lib_containers_ranges | #ifdef __cpp_lib_containers_ranges | ||
− | + | container.insert_range_after(pos, rg); | |
#else | #else | ||
− | + | container.insert_after(pos, rg.cbegin(), rg.cend()); | |
#endif | #endif | ||
− | assert(std::ranges::equal( | + | assert(std::ranges::equal(container, std::vector{1, 2, -1, -2, -3, 3, 4})); |
} | } | ||
}} | }} |
Latest revision as of 00:44, 2 November 2023
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.
Contents |
[edit] 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.
|
[edit] Return value
An iterator
that points at the copy of the last element inserted into forward_list
or at pos if rg is empty.
[edit] Complexity
Linear in size of rg.
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L | (C++23) | Ranges-aware construction and insertion |
[edit] 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})); }
[edit] See also
(C++23) |
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) |