C++ std::list::splice

The std::list::splice function transfers elements from one std::list to another. It does not copy or move the elements but directly manipulates the internal structure of the lists, making it a very efficient operation. The function offers flexibility by allowing you to transfer all elements, a single element, or a range of elements.


Syntax of std::list::splice

</>
Copy
// 1. Splice entire list
void splice(const_iterator position, std::list<T>& x);

// 2. Splice single element
void splice(const_iterator position, std::list<T>& x, const_iterator i);

// 3. Splice range of elements
void splice(const_iterator position, std::list<T>& x, const_iterator first, const_iterator last);

Parameters

ParameterDescription
positionThe position in the target list where elements will be inserted.
xThe source list from which elements will be transferred.
iAn iterator pointing to the single element in x to be transferred.
first, lastIterators defining the range of elements in x to be transferred. The range includes first but excludes last.

Return Value

This function does not return a value. It modifies both the target and source lists by transferring elements between them.

Exceptions

The std::list::splice function does not throw exceptions. It provides a no-throw guarantee because it operates by modifying internal pointers or data structures directly.


Examples for std::list::splice

Example 1: Splicing an Entire List

This example demonstrates how to use splice to transfer all elements from one list to another:

Program

</>
Copy
#include <iostream>
#include <list>

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

    // Splice all elements from list2 into list1 at the beginning
    list1.splice(list1.begin(), list2);

    std::cout << "List 1 contents after splice: ";
    for (const auto& elem : list1) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    std::cout << "List 2 contents after splice: ";
    for (const auto& elem : list2) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation:

  1. A std::list named list1 is initialized with {10, 20, 30}, and list2 is initialized with {40, 50}.
  2. The splice function is called with list1.begin() and list2, transferring all elements from list2 to the beginning of list1.
  3. After the operation, list1 contains all elements, and list2 is empty.

Output:

List 1 contents after splice: 40 50 10 20 30
List 2 contents after splice: 

Example 2: Splicing a Single Element

This example demonstrates how to use splice to transfer a single element from one list to another:

Program

</>
Copy
#include <iostream>
#include <list>

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

    // Splice the first element of list2 into list1 after the first element
    list1.splice(++list1.begin(), list2, list2.begin());

    std::cout << "List 1 contents after splice: ";
    for (const auto& elem : list1) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    std::cout << "List 2 contents after splice: ";
    for (const auto& elem : list2) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation:

  1. list1 is initialized with {10, 20, 30}, and list2 is initialized with {40, 50}.
  2. The splice function transfers the first element (40) of list2 to list1 after its first element (10).
  3. After the operation, list1 contains {10, 40, 20, 30}, and list2 contains {50}.

Output:

List 1 contents after splice: 10 40 20 30
List 2 contents after splice: 50

Example 3: Splicing a Range of Elements

This example demonstrates how to use splice to transfer a range of elements from one list to another:

Program

</>
Copy
#include <iostream>
#include <list>

int main() {
    std::list<int> list1 = {1, 2, 3};
    std::list<int> list2 = {4, 5, 6, 7};

    // Splice a range (second and third elements) from list2 to list1 at the end
    list1.splice(list1.end(), list2, ++list2.begin(), --list2.end());

    std::cout << "List 1 contents after splice: ";
    for (const auto& elem : list1) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    std::cout << "List 2 contents after splice: ";
    for (const auto& elem : list2) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation:

  1. list1 is initialized with {1, 2, 3}, and list2 is initialized with {4, 5, 6, 7}.
  2. The range {5, 6} from list2 is transferred to the end of list1.
  3. After the operation, list1 contains {1, 2, 3, 5, 6}, and list2 contains {4, 7}.

Output:

List 1 contents after splice: 1 2 3 5 6
List 2 contents after splice: 4 7