# multiset::swap() in C++ STL

Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values.

multiset::swap()

This function is used to exchange the contents of two multisets but the sets must be of same type, although sizes may differ.

Syntax :

```multisetname1.swap(multisetname2)
Parameters :
The name of the multiset with which
the contents have to be swapped.

Result :
All the elements of the 2 multiset are swapped.
```

Examples:

```Input  : multiset1 = {1, 2, 3, 4}
multiset2 = {5, 6, 7, 8}
multiset1.swap(multiset2);
Output : multiset1 = {5, 6, 7, 8}
multiset2 = {1, 2, 3, 4}

Input  : multiset1 = {'a', 'b', 'c', 'd'}
multiset2 = {'w', 'x', 'y', 'z'}
multiset1.swap(multiset2);
Output : multiset1 = {'w', 'x', 'y', 'z'}
multiset2 = {'a', 'b', 'c', 'd'}
```

 `// INTEGER MULTISET EXAMPLE ` `// CPP program to illustrate ` `// Implementation of swap() function ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``// Take any two multisets ` `    ``multiset<``int``> multiset1{ 1, 2, 3, 4 }; ` `    ``multiset<``int``> multiset2{ 5, 6, 7, 8 }; ` ` `  `    ``// Swap elements of multisets ` `    ``multiset1.swap(multiset2); ` ` `  `    ``// Print the first multi set ` `    ``cout << ``"multiset1 = "``; ` `    ``for` `(``auto` `it = multiset1.begin(); ` `         ``it != multiset1.end(); ++it) ` `        ``cout << ``' '` `<< *it; ` ` `  `    ``// Print the second multiset ` `    ``cout << endl ` `         ``<< ``"multiset2 = "``; ` `    ``for` `(``auto` `it = multiset2.begin(); ` `         ``it != multiset2.end(); ++it) ` `        ``cout << ``' '` `<< *it; ` ` `  `    ``return` `0; ` `} `

Output:

```multiset1 =  5 6 7 8
multiset2 =  1 2 3 4```

 `// STRING MULTISET EXAMPLE ` `// CPP program to illustrate ` `// Implementation of swap() function ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``// Take any two multisets ` `    ``multiset multiset1{ ``"Geeksforgeeks"` `}; ` ` `  `    ``multiset multiset2{ ``"Computer scince"``, ``"Portal"` `}; ` ` `  `    ``// Swap elements of multisets ` `    ``multiset1.swap(multiset2); ` ` `  `    ``// Print the first multi set ` `    ``cout << ``"multiset1 = "``; ` `    ``for` `(``auto` `it = multiset1.begin(); ` `         ``it != multiset1.end(); ++it) ` `        ``cout << ``' '` `<< *it; ` ` `  `    ``// Print the second multiset ` `    ``cout << endl ` `         ``<< ``"multiset2 = "``; ` `    ``for` `(``auto` `it = multiset2.begin(); ` `         ``it != multiset2.end(); ++it) ` `        ``cout << '`

Output:

```multiset1 =  Computer science portal
multiset2 =  Geeksforgeeks```

 `// CHARACTER MULTISET EXAMPLE ` `// CPP program to illustrate ` `// Implementation of swap() function ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``// Take any two multisets ` `    ``multiset<``char``> multiset1{ ``'A'``, ``'B'``, ``'C'` `}; ` `    ``multiset<``char``> multiset2{ ``'G'``, ``'H'``, ``'I'` `}; ` ` `  `    ``// Swap elements of multisets ` `    ``multiset1.swap(multiset2); ` ` `  `    ``// Print the first multi set ` `    ``cout << ``"multiset1 = "``; ` `    ``for` `(``auto` `it = multiset1.begin(); ` `         ``it != multiset1.end(); ++it) ` `        ``cout << ``' '` `<< *it; ` ` `  `    ``// Print the second multiset ` `    ``cout << endl ` `         ``<< ``"multiset2 = "``; ` `    ``for` `(``auto` `it = multiset2.begin(); ` `         ``it != multiset2.end(); ++it) ` `        ``cout << ``' '` `<< *it; ` ` `  `    ``re`

Output:

```multiset1 =  G H I
multiset2 =  A B C```

Time complexity: Constant

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.