C++ std swap()
C++ std swap(set) is a non-member function of set in C++. This is used to swap (or exchange) the contents of two sets (i.e. x and y) but both the sets must be of same type although sizes may differ.
x: First set object.
y: Second set object of the same type.
All iterators, references and pointers referring to elements in both containers remain valid.
Note that the end iterators do not refer to elements and may be invalidated.
Both containers x and y are modified.
No contained elements are accessed by the call.
This function does not throw an exception.
Let's see the simple example to swap the element of one set to another:
Set contains following elements a b c d
In the above example, set m1 has five elements and m2 is empty. When you swap m1 to m2 then all the elements of m1 is swapped to m2.
Let's see a simple example to exchange the contents of two sets:
set1 contains: 110 220 330 set2 contains: 100 200
In the above example, contents of two sets i.e. set1 and set2 are exchanged to each other.
Let's see a simple example to swap the contents of two sets:
first contains: 20 25 32 second contains: 10 12 75
Let's see a simple example:
Exchange m1 and m2. Contents of m2: 100 200 300 Contents of m1: m1 is now empty.
In the above example, contents of set m1 are swapped to set m2 and after swapping m1 set have been cleared.