C++ List Swapping
C++ List Swapping

1. Introduction to C++ List Swapping

In any programming language, we may end up having to swap two variable contents. If it is a single entity, say two integers. we will end up swapping two values. But, with collections swapping, we must iterate the entire contents and swap the elements between them! The C++ List class of the standard template library provides the function “swap()”, which performs the swapping between two lists. In this article, we will see how it works.

2. Global Function to Iterate Through C++ List

Our Print_List() global function will take the C++ Standard List as a parameter and iterates through each element. On each iteration, it will take the element stored in it and prints that in the console output window. The code for this function is given below:

3. Swap C++ Lists Code Example – Explained

3.1 Create Two Lists

In the main() function, we create two standard C++ Lists. We will swap the content of these two lists in this example. The code snippet is below:

3.2 Populate the Lists

Once the lists are ready, we populate them with the integer values. We populate the first list with Odd Number values and length of this list is Five. The second list is populated with Even Number values and the length of this list is Four. To populate both the list we used push_back() function of standard c++ list. The code is below:

3.3 Print C++ List Contents Before Swapping

Now, we have two C++ standard lists on hand. Before we perform the Swap operation on the standard list, we will print the content of the lists on the console output window. Our global function Print_List() is called twice to print both the list contents. On each call, we are passing the list to be printed on the console output window as a parameter. Below is the code:

3.4 Perform C++ List Swap

Now, it is time to call the swap function. The swap() is a member function of the C++ standard List and which will swap the content of the passed-in list with its own list. For Example, if we call the swap member function on FirstList by passing the SecondList as a parameter, it swaps the contents of the first list with the content of the second list. Therefore, the below two function call swaps the lists and resets it:

In our example, we are calling the swap function of the first list and the second list is passed as a parameter. After we perform the swapping, we are making a call to the Print_List() function to print the contents of the List. Have a look at the below depiction:

C++ List Swapping
C++ List Swapping

The above picture shows the swapping of the list elements between two lists. When swapping occurs, the function also adjusts the list size. In our case, the length of the first list becomes 4 and length of the second list becomes 5. The code that performs swapping and prints the resultant list is  below:

Below is the complete example of C++ Standard List Swapping:

Program Output

C++ List Swapping Program Output
C++ List Swapping Program Output

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: