# Swap two sets (set::swap) in C++ STL

#### Software Engineering C++

Get FREE domain for 1st year and build your brand new site

Reading time: 20 minutes | Coding time: 5 minutes

In this article, we will take a look into swapping two set containers in C++ Standard Template Library using swap method.

Before going into the swap function, let us go through a brief review of Set container in C++.

Set is a data structure that stores elements. It is a simple yet effective data structure when used correctly. In C++'s Standard Template library (STL), we have set as a container that is used to store the values which are unique i.e no value in a set can be repeated or edited. If you want to edit the value added by you the only way is to remove the wrong element and add the correct one.

It is defined as:

set<int> set1{ 1, 2, 3, 4 };


set::swap() is used to exchange the contents between two sets the only conditions to complete the function is that the sets must be of same type, although sizes can be different.

Syntax:

set1.swap(set2)

• Return : None

where set1 and set2 are two different sets.

Note:

• set1 and set2 should be of same datatype
• set1 and set2 can have different size

Time complexity : Constant

### Example 1

In this example, there are 2 sets set1 and set2 with int datatype and size of set are 4. The contents of both the sets are exchanged by using the swap function.

#include <bits/stdc++.h>
using namespace std;

int main()
{

set<int> set1{ 1, 2, 3, 4 };
set<int> set2{ 5, 6, 7, 8 };

// Swap elements of sets
set1.swap(set2);

cout << "set1 : ";
for (auto it = set1.begin();
it != set1.end(); ++it)
cout << ' ' << *it;

cout << endl
<< "set2 : ";
for (auto it = set2.begin();
it != set2.end(); ++it)
cout << ' ' << *it;

return 0;
}


Output:

set1 :  5 6 7 8
set2 :  1 2 3 4


### Example 2

In this example, there is no output as the datatype of both the sides are different and hence violating the condition for swap function to implement.

#include <bits/stdc++.h>
using namespace std;

int main()
{

set<char> set1{'a','b','c','d' };
set<int> set2{ 5, 6, 7, 8 };

// Swap elements of sets
set1.swap(set2);

cout << "set1 :";
for (auto it = set1.begin();
it != set1.end(); ++it)
cout << ' ' << *it;

cout << endl
<< "set2 : ";
for (auto it = set2.begin();
it != set2.end(); ++it)
cout << ' ' << *it;

return 0;
}


Output:

Error : Two sets are of different types


### Example 3

In this example, there are 2 sets set1 and set2 with char datatype and size of set both the sets are different 4 and 3 respectively. The contents of both the sets are exchanged by usibg the swap function.

#include <bits/stdc++.h>
using namespace std;

int main()
{

set<char> set1{'a','b','c','d' };
set<int> set2{'z', 'y', 'z' };

// Swap elements of sets
set1.swap(set2);

cout << "set1 :";
for (auto it = set1.begin();
it != set1.end(); ++it)
cout << ' ' << *it;

cout << endl
<< "set2 : ";
for (auto it = set2.begin();
it != set2.end(); ++it)
cout << ' ' << *it;

return 0;
}


Output:

set1 : x y z
set2 : a b c d


With this, you will have the complete knowledge of using swap function of set container in C++.

#### Harshita Sahai

Maintainer at OpenGenus | Previously Software Developer, Intern at OpenGenus (June to August 2019) | B.Tech in Information Technology from Guru Gobind Singh Indraprastha University (2017 to 2021)

Vote for Harshita Sahai for Top Writers 2021: