forward_list::swap() in C++ STL

The forward_list::swap() is a built-in function in CPP STL which exchanges the contents of the first given forward_list with another forward_list.

Syntax:

swap(forward_list first, forward_list second)
               or 
forward_list1.swap(forward_list second)

Parameters: The function accepts two parameters which are specified as below:



  • first – The first forward_list
  • second – The second forward_list

Return value:The function does not return anything. It swaps both the lists.

Below programs demonstrate the function mentioned above:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program that demonstrates the
// forward_list::swap() function
// when two parameters is passed
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialising the two forward_list
    forward_list<int> firstlist = { 9, 8, 7, 6 };
  
    forward_list<int> secondlist = { 10, 20, 30 };
  
    // printing elements before the swap operation
    // for firstlist and secondlist
    cout << "Before swap operation firstlist was: ";
    for (auto it = firstlist.begin(); it != firstlist.end(); ++it)
        cout << *it << " ";
  
    cout << "\nBefore swap operation secondlist was: ";
    for (auto it = secondlist.begin(); it != secondlist.end(); ++it)
        cout << *it << " ";
  
    // swap operation on two lists
    swap(firstlist, secondlist);
  
    // printing elements after the swap operation
    // for forward1 and secondlist
    cout << "\n\nAfter swap operation firstlist is: ";
  
    for (auto it = firstlist.begin(); it != firstlist.end(); ++it)
        cout << *it << " ";
    cout << "\nAfter swap operation secondlist is:";
    for (auto it = secondlist.begin(); it != secondlist.end(); ++it)
        cout << *it << " ";
    return 0;
}

chevron_right


Output:

Before swap operation firstlist was: 9 8 7 6 
Before swap operation secondlist was: 10 20 30 

After swap operation firstlist is: 10 20 30 
After swap operation secondlist is:9 8 7 6

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program that demonstrates the
// forward_list::swap() function
// when two parameters is passed
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialising the two forward_list
    forward_list<int> firstlist = { 9, 8, 7, 6 };
  
    forward_list<int> secondlist = { 10, 20, 30 };
  
    // printing elements before the swap operation
    // for firstlist and secondlist
    cout << "Before swap operation firstlist was: ";
    for (auto it = firstlist.begin(); it != firstlist.end(); ++it)
        cout << *it << " ";
  
    cout << "\nBefore swap operation secondlist was: ";
    for (auto it = secondlist.begin(); it != secondlist.end(); ++it)
        cout << *it << " ";
  
    // swap operation on two lists
    firstlist.swap(secondlist);
  
    // printing elements after the swap operation
    // for forward1 and secondlist
    cout << "\n\nAfter swap operation firstlist is: ";
  
    for (auto it = firstlist.begin(); it != firstlist.end(); ++it)
        cout << *it << " ";
    cout << "\nAfter swap operation secondlist is:";
    for (auto it = secondlist.begin(); it != secondlist.end(); ++it)
        cout << *it << " ";
    return 0;
}

chevron_right


Output:

Before swap operation firstlist was: 9 8 7 6 
Before swap operation secondlist was: 10 20 30 

After swap operation firstlist is: 10 20 30 
After swap operation secondlist is:9 8 7 6


My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.