C++ List swap() Function

Last Updated : 14 May 2026

List swap() Function

In C++, the list::swap() function is used to exchange the contents of one list with another list of the same type. It is a member function of the Standard Template Library (STL) list container and performs the swapping operation efficiently in constant time complexity O(1). Instead of copying individual elements, the function swaps the internal pointers of the lists.

C++ List swap() Function

Syntax

It has the following syntax:

Parameters

The list1 and list2 must both be std::list objects (with the same data type and allocator).

Return Value

The swap() function does not return any value. It returns only void.

Examples of List swap() Function

Here, we are going to discuss several examples to demonstrate the List swap() Function

Example 1: Swap Elements Between Two Lists

This example demonstrates how to swap the contents of two lists using the swap() function.

Output:

Before swapping:
list1: 10 20 30 
list2: 100 200 300 400 

After swapping:
list1: 100 200 300 400 
list2: 10 20 30

Explanation:

In this example, we have created two lists with their elements: List 1 {10, 20, 30} and List 2{100, 200, 300, 400}. First, it will print out the list contents before swapping. Upon executing the line list1.swap(list2), the items in lists 1 and 2 are reversed. After swapping, the original material in list 1 becomes the new material for list 2, and the original material in list 2 becomes the new material for list 1. This operation is performed in a constant time (O(1)).

Types of errors or exceptions occur in the C++ List Swap()

There are several types of errors and exceptions that occur in the list swap() function in C++. Some of them are as follows:

1) Type Mismatch Error (Compilation Error)

We cannot swap two lists that are of different types.

The compiler generates a type mismatch error because the two lists must be of the same type.

2) Allocator Mismatch Error (Compilation/Linking Error)

If both lists were created with different allocators, the swap() function can fail or cause unpredictable behavior. The two lists must have the same type of allocator, the same way type is checked for lists themselves.

3) Exception Safety

In C++, the list::swap() member function does not throw (noexcept in modern C++). Therefore, we do not have to worry about runtime exceptions (such as std::bad_alloc) for several operations and functions in the STL.

4) Logic Errors (Programmer Errors)

In C++, it is possible to use the swap() function incorrectly, which results in logic errors, not compilation/runtime errors.

5) Infrequently Occurring Behavior

In general, there is no reason to swap a list with itself, but if we try, the compiler will allow it (list1.swap(list1).

Example 1: Swap Two Lists of Strings Using the list::swap() Function

This example demonstrates how to swap the contents of two string lists using the list::swap() function.

Output:

Before swapping:
Fruits: Apple, Banana, Mango 
Vegetables: Carrot, Potato, Tomato, Onion 

After swapping:
Fruits: Carrot, Potato, Tomato, Onion 
Vegetables: Apple, Banana, Mango

Explanation:

In this example, we have taken two different lists of fruits and vegetables. First, this program will print the content of these two lists before swapping. When we execute the Fruits.swap(vegetables) statement, both lists are switched in a fixed amount of time. After swapping, the names of every fruit will show up in the vegetables list, and the names of every vegetable will show up in the fruits.

Example 2: Swap Character Lists Using the swap() Function

This example demonstrates how to exchange the elements of two character lists using the swap() function.

Output:

Initially, the content of the list li is: +-*@
Initially, the content of the list li1 is: List
After swapping, the content of the list li is: List
After swapping, the content of the list li1 is: +-*@

Explanation:

In this example, we have created two character lists: li containing '+', '-', '*', '@', and li1 containing 'L', 'i', 's', 't'. First, the program will print the contents of both lists before swapping. When the li.swap(li1) statement is executed, the elements of the two lists are exchanged in constant time (O(1)). After the swap() operation, li contains the original elements of li1, and li1 contains the original elements of li.