C++ std::list::swap

The std::list::swap function exchanges the contents of two std::list containers. After the swap, the first list contains the elements of the second list, and vice versa. This operation is very efficient because it swaps the internal data structures of the lists without moving or copying individual elements.


Syntax of std::list::swap

</>
Copy
void swap(std::list<T>& other);

Parameters

ParameterDescription
otherA reference to another std::list container whose contents will be swapped with the current list.

Return Value

This function does not return a value. It modifies the contents of both lists by exchanging their data structures.

Exceptions

The std::list::swap function does not throw exceptions. It provides a no-throw guarantee since the operation only involves swapping internal pointers or data structures of the lists.


Examples for std::list::swap

Example 1: Swapping Two Lists

This example demonstrates how to use swap to exchange the contents of two lists:

Program

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

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

    // Swap the contents of list1 and list2
    list1.swap(list2);

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

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

    return 0;
}

Explanation:

  1. Two std::list containers named list1 and list2 are created with different sets of elements.
  2. The swap function is called to exchange the contents of the two lists.
  3. After the swap, list1 contains the elements of list2, and list2 contains the elements of list1.
  4. The program prints the contents of both lists to verify the operation.

Output:

List 1 contents after swap: 10 20 30 40
List 2 contents after swap: 1 2 3

Example 2: Swapping Lists of Different Sizes

This example demonstrates how swap works with lists of different sizes:

Program

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

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

    // Swap the contents of list1 and list2
    list1.swap(list2);

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

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

    return 0;
}

Explanation:

  1. Two std::list containers, list1 and list2, are created with different sizes and elements.
  2. The swap function is used to exchange the contents of the two lists.
  3. After the swap, list1 contains all the elements of list2, and list2 contains all the elements of list1.
  4. The contents of both lists are printed to verify the swap operation.

Output:

List 1 contents after swap: 1 2 3 4 5
List 2 contents after swap: 5 10

Example 3: Using std::swap for Lists

This example demonstrates how to use the std::swap algorithm to swap two lists:

Program

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

int main() {
    std::list<int> list1 = {100, 200};
    std::list<int> list2 = {300, 400, 500};

    // Use std::swap to exchange contents of the lists
    std::swap(list1, list2);

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

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

    return 0;
}

Explanation:

  1. The std::swap algorithm is used to exchange the contents of two lists.
  2. Internally, std::swap calls the swap member function of the std::list class.
  3. The contents of both lists are printed to verify the swap operation.

Output:

List 1 contents after swap: 300 400 500
List 2 contents after swap: 100 200