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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//ListSort01: Required Includes #include <stdio.h> #include <conio.h> #include <iostream> #include <list> #include <string> using namespace std; //ListSort 02: Function to Print the Values in List void Print_List(list<int> listParam) { list<int>::iterator listItr; printf("\nThe List members are:\n"); for (listItr = listParam.begin(); listItr != listParam.end(); listItr++) { printf("%d ", *listItr); } } |
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:
1 2 3 |
//3.1 Create lists list<int> FirstList; list<int> SecondList; |
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
//3.2 Prepare First List FirstList.push_back(1); FirstList.push_back(3); FirstList.push_back(5); FirstList.push_back(7); FirstList.push_back(9); //3.3 Prepare Second List SecondList.push_back(2); SecondList.push_back(4); SecondList.push_back(6); SecondList.push_back(8); |
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:
1 2 3 4 5 |
//3.4 Print First and Second List printf("First List"); Print_List(FirstList); printf("\nSecond List"); Print_List(SecondList); |
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:
1 2 |
FirstList.swap(SecondList); SecondList.swap(FirstList); |
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
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:
1 2 3 4 5 6 7 8 9 10 |
//3.5 Swap the Lists and Print Contents FirstList.swap(SecondList); SecondList.swap(FirstList); printf("\n\nLists are swapped\n"); printf("-----------------\n"); printf("First List"); Print_List(FirstList); printf("\nSecond List"); Print_List(SecondList); |
Below is the complete example of C++ Standard List Swapping:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
//ListSort01: Required Includes #include <stdio.h> #include <conio.h> #include <iostream> #include <list> #include <string> using namespace std; //ListSort 02: Function to Print the Values in List void Print_List(list<int> listParam) { list<int>::iterator listItr; printf("\nThe List members are:\n"); for (listItr = listParam.begin(); listItr != listParam.end(); listItr++) { printf("%d ", *listItr); } } //ListSort 03: Swapping Two Lists void main() { //3.1 Create lists list<int> FirstList; list<int> SecondList; //3.2 Prepare First List FirstList.push_back(1); FirstList.push_back(3); FirstList.push_back(5); FirstList.push_back(7); FirstList.push_back(9); //3.3 Prepare Second List SecondList.push_back(2); SecondList.push_back(4); SecondList.push_back(6); SecondList.push_back(8); //3.4 Print First and Second List printf("First List"); Print_List(FirstList); printf("\nSecond List"); Print_List(SecondList); //3.5 Swap the Lists and Print Contents FirstList.swap(SecondList); SecondList.swap(FirstList); printf("\n\nLists are swapped\n"); printf("-----------------\n"); printf("First List"); Print_List(FirstList); printf("\nSecond List"); Print_List(SecondList); _getch(); } |
Program Output

C++ List Swapping Program Output
Categories: C++
Tags: C++ List, list::swap