ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

Swapping Two Standard C++ List Through std::list::swap() With Example

Updated on March 26, 2018
sirama profile image

I am a software engineer. I have been working with C++, MFC, and .net technologies for 15 Yrs. I like playing video games & reading books.

1. Introduction to Swapping

In any programming language, we may end up having to swap the content of the two variables. If it is a single entity, say two integers. we will end up swapping two values. But, in the case of collections swapping, we must iterate the entire contents and swap the elements between them! The list class of the C++ 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 each element

The Print_List() global function will take the C++ Standard List as 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:

//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 two Standard List - Code Example - Explained

In the main() function, two c++ standard lists are created. We are going to swap the content of these two lists in this example. The code snippet is below:

//3.1 Create lists
list<int> FirstList;
list<int> SecondList;

Once the lists are ready, they are populated with the integer values. The first list is populated 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.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);

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 the content of both the lists. On each call, we are passing the list to be printed on the console output window as parameter. Below is the code:

//3.4 Print First and Second List
printf("First List");
Print_List(FirstList);
printf("\nSecond List");
Print_List(SecondList);

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 parameter, the contents of the first list is swapped with the content of the second list. Therefore, the below two function call achieve the same result:

FirstList.swap(SecondList);
SecondList.swap(FirstList);

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

Swapping two Standard C++ List
Swapping two Standard C++ List | Source

The above picture shows the swapping of the list elements between two lists. When swapping occurs, the list size also gets adjusted. 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 given below:

//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:

//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();
}

The output of the above program Example is displayed in the below screen-shot:

Program Output of C++ Standard List Swapping
Program Output of C++ Standard List Swapping | Source

© 2018 sirama

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working