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.

It has the following syntax:
The list1 and list2 must both be std::list objects (with the same data type and allocator).
The swap() function does not return any value. It returns only void.
Here, we are going to discuss several examples to demonstrate the List swap() Function
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)).
There are several types of errors and exceptions that occur in the list swap() function in C++. Some of them are as follows:
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.
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.
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.
In C++, it is possible to use the swap() function incorrectly, which results in logic errors, not compilation/runtime errors.
In general, there is no reason to swap a list with itself, but if we try, the compiler will allow it (list1.swap(list1).
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.
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.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India