list swap( ) in C++ STL


Given is the task to show the functionality list swap( ) function in C++ in STL.

What is List in STL?

List are containers that allow constant time insertion and deletion anywhere in sequence. List are implemented as doubly linked lists. List allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.

What is swap( )?

This function is used to swap the elements of one list with another list and both having same data type and size.

Syntax: listname1.swap(listname2)

Example

Input List1: 50 60 80 90
List2: 90 80 70 60

Output After swapping operation
List1: 90 80 70 60
List2: 50 60 80 90
Input List1: 45 46 47 48 49
List2: 50 51 52 53 54

Output After swapping Operation
List1: 50 51 52 53 54
List2: 45 46 47 48 49

Approach can be followed

  • First we initialize the two List.

  • Then we print the two List.

  • Then we define swap( ) function.

  • At last we print two list after swapping operation.

By using the above approach we can swap two list.

Example

// C++ code to demonstrate the working of list swap( ) function in STL
#include<iostream.h>
#include<list.h>
Using namespace std;
int main ( ){
   // initializing two lists
   List<int> list1 = { 10, 20, 30, 40, 50 };
   cout<< “ List1: “;
   for( auto x = list1.begin( ); x != list1.end( ); ++x)
      cout<< *x << “ “;
   List<int> list2 = { 40, 50, 60, 70, 80 };
   cout<< “ List2: “;
   for( auto x = list2.begin( ); x != list2.end( ); ++x)
   cout<< *x << “ “;
   // defining swap( ) function
   list1.swap(list2);
   cout<< “ After swapping List1 is :”;
   for(auto x = list1.begin( ); x != list1.end( ); ++x)
      cout<< *x<< “ “;
   cout<< “ After swapping List2 is :”;
   for(auto x = list1.begin( ); x!= list2.end( ); ++x)
      cout<< *x<< “ “;
   return 0;
}

Output

If we run the above code then it will generate the following output

Input - List1: 10 20 30 40 50
List2: 40 50 60 70 80

Output - After swapping List1 is: 40 50 60 70 80
After swapping List2 is: 10 20 30 40 50

Example

// C++ code to demonstrate the working of list swap( ) function in STL
#include<iostream.h>
#include<list.h>
Using namespace std;
int main ( ){
   // initializing two lists
   list<int> list1 = { 11, 12, 13, 14, 15 };
   cout<< “ List1: “;
   for( auto x = list1.begin( ); x != list1.end( ); ++x)
      cout<< *x << “ “;
   List<int> list2 = { 16, 17, 18, 19, 20 };
   cout<< “ List2: “;
   for( auto x = list2.begin( ); x != list2.end( ); ++x)
      cout<< *x << “ “;
   // defining swap( ) function
   list1.swap(list2);
   cout<< “ After swapping List1 is :”;
   for(auto x = list1.begin( ); x != list1.end( ); ++x)
   cout<< *x<< “ “;
   cout<< “ After swapping List2 is :”;
   for(auto x = list1.begin( ); x!= list2.end( ); ++x)
      cout<< *x<< “ “;
   return 0;
}

Output

If we run the above code then it will generate the following output

Input - List1: 11 12 13 14 15
   List2: 16 17 18 19 20
Output - After swapping List1 is: 16 17 18 19 20
   After swapping List2 is: 11 12 13 14 15

Updated on: 05-Mar-2020

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements