[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/container/list/splice [Back]  [Original]

std::list::splice cppreference.com
cppreference.com

std::list::splice

cppreference.com

<metanoindex/>

<tbody> </tbody>
void splice(const_iterator pos, list& other);
(1)
void splice(const_iterator pos, list&& other);
(1) ( C++11)
void splice(const_iterator pos, list& other, const_iterator it);
(2)
void splice(const_iterator pos, list&& other, const_iterator it);
(2) ( C++11)
void splice(const_iterator pos, list& other, const_iterator first, const_iterator last);
(3)
void splice(const_iterator pos, list&& other, const_iterator first, const_iterator last);
(3) ( C++11)
.
:
Moves elements from one list to another.
Google.
. .
. , : get_allocator() != other.get_allocator(). , *this, other.
:
No elements are copied. The behavior is undefined if: get_allocator() != other.get_allocator(). No iterators or references become invalidated, the iterators to moved elements now refer into *this, not into other.
Google.
. .

1)

other *this. , pos. other . , this == &other.
:
Moves all elements from other into *this. The elements are inserted before the element pointed to by pos. The container other becomes empty after the operation. The behavior is undefined if this == &other.
Google.
. .

2)

, it other *this. , pos.
:
Moves the element pointed to by it from other into *this. The element is inserted before the element pointed to by pos.
Google.
. .

3)

[first, last) other *this. , pos. , pos [first,last).
:
Moves the elements in the range [first, last) from other into *this. The elements are inserted before the element pointed to by pos. The behavior is undefined if pos is an iterator in the range [first,last).
Google.
. .

pos
,
:
element before which the content will be inserted
Google.
. .
other
,
:
another container to move the content from
Google.
. .
it
, other *this
:
the element to move from other to *this
Google.
. .
first, last
, other *this
:
the range of elements to move from other to *this
Google.
. .

()

1-2)

.
:
Constant.
Google.
. .

3)

this == &other, std::distance(first, last).
:
Constant if this == &other, otherwise linear in std::distance(first, last).
Google.
. .

#include <iostream>
#include <list>

std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list)
{
    for (auto &i : list) {
        ostr << " " << i;
    }
    return ostr;
}

int main ()
{
    std::list<int> list1 = { 1, 2, 3, 4, 5 };
    std::list<int> list2 = { 10, 20, 30, 40, 50 };

    auto it = list1.begin();
    std::advance(it, 2);

    list1.splice(it, list2);

    std::cout << "list1: " << list1 << "\n";
    std::cout << "list2: " << list2 << "\n";

    list2.splice(list2.begin(), list1, it, list1.end());

    std::cout << "list1: " << list1 << "\n";
    std::cout << "list2: " << list2 << "\n";
}

:

list1:  1 2 10 20 30 40 50 3 4 5
list2:
list1:  1 2 10 20 30 40 50
list2:  3 4 5

.


(public -) []
,
(public -) []

Web Proxy Viewer  |  New URL  |  Original Page