C++ std swap
C++ std swap(set) is an external function in C++ for sets. It is designed to interchange the elements of two sets (e.g. x and y), provided that both sets are of identical types, even if their sizes vary.
Syntax
template <class T, class Compare, class Alloc>
void swap (set<T,Compare,Alloc>& x, set<T,Compare,Alloc>& y);
Parameter
x : First set object.
y : Second set object of the same type.
Return value
Complexity
Constant.
Iterator validity
All iterators, references, and pointers pointing to elements within both containers will continue to be valid.
Note that the final iterators do not point to elements and can become invalid.
Data Races
Both containers x and y are modified.
No contained elements are accessed by the call.
Exception Safety
This function does not throw an exception.
Example 1
Let's explore a basic example demonstrating the swapping of elements between two sets:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
set<char> m1 = {'a','b','c','d'};
set<char> m2;
swap(m1, m2);
cout << "Set contains following elements" << endl;
for (auto it = m2.begin(); it != m2.end(); ++it)
cout << *it<< endl;
return 0;
}
Output:
Set contains following elements
a
b
c
d
In the previous illustration, set m1 contains five elements while set m2 is devoid of any elements. Upon swapping the contents of m1 with m2, all elements originally in m1 are transferred to m2.
Example 2
Let's examine a basic illustration demonstrating the swapping of elements between two sets:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<int> set1,set2;
set1= {100,200};
set2 = {110, 220, 330};
swap(set1,set2);
cout << "set1 contains:\n";
for (set<int>::iterator it=set1.begin(); it!=set1.end(); ++it)
cout << *it<< '\n';
cout << "set2 contains:\n";
for (set<int>::iterator it=set2.begin(); it!=set2.end(); ++it)
cout << *it<< '\n';
return 0;
}
Output:
set1 contains:
110
220
330
set2 contains:
100
200
In the provided example, the elements of two sets, namely set1 and set2, are swapped with each other.
Example 3
Let's consider a basic example to exchange the elements of two sets:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
int myints[]={12,75,10,32,20,25};
set<int> first (myints,myints+3); // 10,12,75
set<int> second (myints+3,myints+6); // 20,25,32
swap(first,second);
cout << "first contains:";
for (set<int>::iterator it=first.begin(); it!=first.end(); ++it)
cout << ' ' << *it;
cout << '\n';
cout << "second contains:";
for (set<int>::iterator it=second.begin(); it!=second.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
Output:
first contains: 20 25 32
second contains: 10 12 75
Example 4
Let's see a simple example:
#include <iostream>
#include <string>
#include <set>
using namespace std;
void show(const char *msg, set<int> mp);
int main() {
set<int> m1, m2;
m1.insert(100);
m1.insert(300);
m1.insert(200);
// Exchange the contents of m1 and m2.
cout << "Exchange m1 and m2.\n";
swap(m1,m2);
show("Contents of m2: ", m2);
show("Contents of m1: ", m1);
// Clear m1.
m1.clear();
if(m1.empty()) cout << "m1 is now empty.";
return 0;
}
// Display the contents of a set<string, int> by using an iterator.
void show(const char *msg, set<int> mp) {
set<int>::iterator itr;
cout << msg << endl;
for(itr=mp.begin(); itr != mp.end(); ++itr)
cout << " " << *itr<< endl;
cout << endl;
}
Output:
Exchange m1 and m2.
Contents of m2:
100
200
300
Contents of m1:
m1 is now empty.
In the previous scenario, the elements from set m1 have been exchanged with those in set m2, resulting in an empty set m1 after the swap operation.