std::list<T,Allocator>::splice

Transfers elements from one list to another.

# Declarations

void splice( const_iterator pos, list& other );
void splice( const_iterator pos, list&& other );

(since C++11)

void splice( const_iterator pos, list& other, const_iterator it );
void splice( const_iterator pos, list&& other, const_iterator it );

(since C++11)

void splice( const_iterator pos, list& other,
const_iterator first, const_iterator last);
void splice( const_iterator pos, list&& other,
const_iterator first, const_iterator last );

(since C++11)

# Parameters

# Return value

(none)

# Example

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

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 250C++98references and iterators to the movedelement(s) were all invalidatedthey refer or point to thesame element(s) in *this
N2525C++98O(1) splicing could not be guaranteed ifget_allocator() != other.get_allocator()the behavior isundefined in this case

# See also