Przestrzenie nazw
Warianty

std::list::splice

Z cppreference.com
<tbody> </tbody>
void splice( const_iterator pos, list& other );
(1)
void splice( const_iterator pos, list&& other );
(1) (od C++11)
void splice( const_iterator pos, list& other, const_iterator it );
(2)
void splice( const_iterator pos, list&& other, const_iterator it );
(2) (od 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) (od C++11)

Przenosi elementy z jednej listy do drugiej.

Nie są wykonywane żadne operacje kopiowania ani przenoszenia na poszczególnych elementach. Przeniesienie następuję przez podmianę wartości wewnętrznych wskaźników w wierzchołkach listy. Zachowanie jest niezdefiniowane, jeśli: get_allocator() != other.get_allocator(). Żadne iteratory ani referencje nie zostają unieważnione, jednak iteratory wskazujące na przeniesione elementy odnoszą się teraz do *this, a nie do other.

1) Przenosi wszystkie elementy z other do *this. Elementy są wstawione bezpośrednio przed elementem wskazywanym przez pos. Kontener other zostaje opróżniony w wyniku tej operacji. Zachowanie jest niezdefiniowane, jeśli this == &other.
2) Przenosi element wskazywany przez it z other do *this. Element ten zostaje wstawiony bezpośrednio przed elementem wskazywanym przez pos.
3) Przenosi elementy z przedziału [first, last) z other do *this. Elementy są wstawione bezpośrednio przed elementem wskazywanym przez pos. Zachowanie jest niezdefiniowane, jeśli pos jest iteratorem wewnątrz przedziału [first,last).

Parametry

pos - element, przed którym zostanie wstawiona nowa zawartość
other - inny kontener, z którego zostanie przeniesiona zawartość
it - element do przeniesienia z other do *this
first, last - przedział z other, z którego elementy zostaną przeniesione do *this

Zwracana wartość

(brak)

Złożoność

1-2) Stała.

3) Stała, jeśli this == &other, w przeciwnym wypadku liniowa względem std::distance(first, last).

Przykład

#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";
}

Wynik:

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

Zobacz także

scala dwie posortowane listy
(publiczna metoda) [edit]
usuwa elementy spełniające wskazane kryteria
(publiczna metoda) [edit]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.