C++ List::swap() Function



The C++ std::list::swap() function is used to exchange the contents of the list with those of the other. It does not invoke any move, copy, or swap operations on an individual list elements.

In C++, the swapping is nothing but an exchanging functionality that is used to exchange data with another one. The swap() function ignores the list size while swapping the list elements, but it verifies the list type. If the list type is different, it will raise an error. The return type of this function is void which implies that it does not return any value. It changes the list size if necessary.

Syntax

Following is the syntax of the C++ std::list::swap() function −

void swap( list& other );

Parameters

other − It is an another list object of same type.

Return Value

This function does not return any value.

Example 1

In the following program, we are using the C++ std::list::swap() function to swap the specified list(type int) elements {1, 2, 3, 4, 5} with the current list elements {10, 20, 30, 40}.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create an int list
   list<int> list1 = {10, 20, 30, 40};
   cout<<"First list elements before swap: "<<endl;
   for(int l1 : list1) {
      cout<<l1<<" ";
   }
   list<int> list2 = {1, 2, 3, 4, 5};
   cout<<"\nSecond list elements before swap: "<<endl;
   for(int l2: list2) {
      cout<<l2<<" ";
   }
   //using the swap() function
   list1.swap(list2);
   cout<<"\nFirst list elements after swap: "<<endl;
   for(int l1 : list1) {
      cout<<l1<<" ";
   }
   cout<<"\nSecond list elements after swap: "<<endl;
   for(int l2 : list2) {
      cout<<l2<<" ";
   }
}

Output

Following is the output of the above program −

First list elements before swap: 
10 20 30 40 
Second list elements before swap: 
1 2 3 4 5 
First list elements after swap: 
1 2 3 4 5 
Second list elements after swap: 
10 20 30 40 

Example 2

Following is another example of the C++ std::list::swap() function. Here, we are creating two lists (type char) named upper_case_list and lower_case_list with the values {'A', 'B', 'C', 'D'} and {'a', 'b', 'c', 'd'}. Then, using the swap() function, we are trying to swap the upper_case_list elements with the lower_case_list elements.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create list
   list<char> upper_case_list  = {'A', 'B', 'C', 'D'};
   cout<<"First list elements before swap: "<<endl;
   for(char u : upper_case_list) {
      cout<<u<<" ";
   }
   list<char> lower_case_list  = {'a', 'b', 'c', 'd'};
   cout<<"\nSecond list elements before swap: "<<endl;
   for(char l: lower_case_list) {
      cout<<l<<" ";
   }
   //using the swap() function
   upper_case_list.swap(lower_case_list);
   cout<<"\nFirst list elements after swap: "<<endl;
   for(char u : upper_case_list) {
      cout<<u<<" ";
   }
   cout<<"\nSecond list elements after swap: "<<endl;
   for(char l : lower_case_list) {
      cout<<l<<" ";
   }
}

Output

On executing the above program, it will produce the following output −

First list elements before swap: 
A B C D 
Second list elements before swap: 
a b c d 
First list elements after swap: 
a b c d 
Second list elements after swap: 
A B C D 

Example 3

Apart from the int and char list elements, you can also swap the string elements in the list(type string).

In this example, we are creating two lists(type string) named names and surnames with the values {"Rahul", "Geeta", "Reeta"} and {"Verma", "Gupta", "Sharma"}. Then, using the swap() function, we are trying to swap the name list elements with the surnames list elements.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create list
   list<string> names  = {"Rahul", "Geeta", "Reeta"};
   cout<<"First list elements before swap: "<<endl;
   for(string n : names){
      cout<<n<<" ";
   }
   list<string> surnames  = {"Verma", "Gupta", "Sharma"};
   cout<<"\nSecond list elements before swap: "<<endl;
   for(string s: surnames){
      cout<<s<<" ";
   }
   //using the swap() function
   names.swap(surnames);
   cout<<"\nFirst list elements after swap: "<<endl;
   for(string n : names){
      cout<<n<<" ";
   }
   cout<<"\nSecond list elements after swap: "<<endl;
   for(string s : surnames){
      cout<<s<<" ";
   }
}

Output

This will generate the following output −

First list elements before swap: 
Rahul Geeta Reeta 
Second list elements before swap: 
Verma Gupta Sharma 
First list elements after swap: 
Verma Gupta Sharma 
Second list elements after swap: 
Rahul Geeta Reeta 
Advertisements