Difference between revisions of "cpp/container/forward list/splice after"
From cppreference.com
< cpp | container | forward list
m |
(Expanded example and "see also" for splicing from the first element) |
||
Line 71: | Line 71: | ||
l2.splice_after(l2.cbegin(), l1, l1.cbegin(), l1.cend()); | l2.splice_after(l2.cbegin(), l1, l1.cbegin(), l1.cend()); | ||
// not equivalent to l2.splice_after(l2.cbegin(), l1); | // not equivalent to l2.splice_after(l2.cbegin(), l1); | ||
+ | // which is equivalent to | ||
+ | // l2.splice_after(l2.cbegin(), l1, l1.cbefore_begin(), l1.end()); | ||
+ | std::cout << "l1: "; | ||
for(int n : l1) | for(int n : l1) | ||
std::cout << n << ' '; | std::cout << n << ' '; | ||
− | std::cout << | + | std::cout << "\nl2: "; |
for(int n : l2) | for(int n : l2) | ||
Line 81: | Line 84: | ||
} | } | ||
| output= | | output= | ||
− | 1 | + | l1: 1 |
− | 10 2 3 4 5 11 12 | + | l2: 10 2 3 4 5 11 12 |
}} | }} | ||
Line 89: | Line 92: | ||
{{dsc inc | cpp/container/dsc merge |forward_list}} | {{dsc inc | cpp/container/dsc merge |forward_list}} | ||
{{dsc inc | cpp/container/dsc remove |forward_list}} | {{dsc inc | cpp/container/dsc remove |forward_list}} | ||
+ | {{dsc inc | cpp/container/dsc before_begin|forward_list}} | ||
{{dsc end}} | {{dsc end}} | ||
Revision as of 03:14, 14 June 2021
void splice_after( const_iterator pos, forward_list& other ); |
(1) | (since C++11) |
void splice_after( const_iterator pos, forward_list&& other ); |
(1) | (since C++11) |
void splice_after( const_iterator pos, forward_list& other, const_iterator it ); |
(2) | (since C++11) |
void splice_after( const_iterator pos, forward_list&& other, const_iterator it ); |
(2) | (since C++11) |
void splice_after( const_iterator pos, forward_list& other, const_iterator first, const_iterator last ); |
(3) | (since C++11) |
void splice_after( const_iterator pos, forward_list&& other, const_iterator first, const_iterator last ); |
(3) | (since C++11) |
Moves elements from another forward_list
to *this.
No elements are copied. pos
must be either a deferenceable valid iterator into *this or the before_begin() iterator (in particular, end() is not a valid argument for pos
). The behavior is undefined if get_allocator() != other.get_allocator(). No iterators or references become invalidated, the iterators to the moved elements now refer into *this, not into other
.
1) Moves all elements from
other
into *this. The elements are inserted after the element pointed to by pos
. The container other
becomes empty after the operation. The behavior is undefined if other
refers to the same object as *this.2) Moves the element pointed to by the iterator following
it
from other
into *this. The element is inserted after the element pointed to by pos
. Has no effect if pos==it
or if pos==++it
.3) Moves the elements in the range
(first, last)
from other
into *this. The elements are inserted after the element pointed to by pos
. The element pointed-to by first
is not moved. The behavior is undefined if pos
is an iterator in the range (first,last)
.Contents |
Parameters
pos | - | element after which the content will be inserted |
other | - | another container to move the content from |
it | - | iterator preceding the iterator to the element to move from other to *this
|
first, last | - | the range of elements to move from other to *this
|
Return value
(none)
Exceptions
Throws nothing.
Complexity
1) Linear in the size of
other
2) Constant
3) Linear in std::distance(first, last)
Example
Demonstrates the meaning of open interval (first, last) in the third form of splice_after(): the first element of l1 is not moved.
Run this code
#include <iostream> #include <forward_list> int main() { std::forward_list<int> l1 = {1,2,3,4,5}; std::forward_list<int> l2 = {10,11,12}; l2.splice_after(l2.cbegin(), l1, l1.cbegin(), l1.cend()); // not equivalent to l2.splice_after(l2.cbegin(), l1); // which is equivalent to // l2.splice_after(l2.cbegin(), l1, l1.cbefore_begin(), l1.end()); std::cout << "l1: "; for(int n : l1) std::cout << n << ' '; std::cout << "\nl2: "; for(int n : l2) std::cout << n << ' '; std::cout << '\n'; }
Output:
l1: 1 l2: 10 2 3 4 5 11 12
See also
merges two sorted lists (public member function) | |
removes elements satisfying specific criteria (public member function) | |
returns an iterator to the element before beginning (public member function) |