The C++ multimap swap function is used to swap (or exchange) the contents of two multimaps but both the multimaps must be of same type although sizes may differ.
Syntax
void swap (multimap& x);
Parameter
x : multimap container to exchange the contents with.
Return value
Complexity
Constant.
Iterator validity
All iterators, references and pointers referring to elements in both containers remain valid, but now are referring to elements in the other container, and iterate in it.
Data races
Both the container and x are modified.
Exception safety
No effect on container if exception is thrown.
Example 1
Let's see the simple example to swap the element of one multimap to another:
#include <iostream>
#include <map>
using namespace std;
int main(void) {
multimap<char, int> m1 = {
{'a', 1},
{'b', 2},
{'c', 3},
{'b', 4},
{'c', 5},
};
multimap<char, int> m2;
m2.swap(m1);
cout << "Multimap m2 contains following elements" << endl;
for (auto it = m2.begin(); it != m2.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
Output:
Multimap m2 contains following elements
a = 1
b = 2
b = 4
c = 3
c = 5
In the above example, multimap m1 has five elements and m2 is empty. When you swap m1 to m2 then all the elements of m1 is swapped to m2.
Example 2
Let's see a simple example to exchange the contents of two multimaps:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char,int> multimap1,multimap2;
multimap1 = {
{'x', 100},
{'x', 200}
};
multimap2 = {
{'a', 110},
{'b', 220},
{'a', 330}
};
multimap1.swap(multimap2);
cout << "multimap1 contains:\n";
for (multimap<char,int>::iterator it=multimap1.begin(); it!=multimap1.end(); ++it)
cout << it->first << " => " << it->second << '\n';
cout << "multimap2 contains:\n";
for (multimap<char,int>::iterator it=multimap2.begin(); it!=multimap2.end(); ++it)
cout << it->first << " => " << it->second << '\n';
return 0;
}
Output:
multimap1 contains:
a => 110
a => 330
b => 220
multimap2 contains:
x => 100
x => 200
In the above example, contents of two multimaps i.e. multimap1 and multimap2 are exchanged to each other.
Example 3
Let's see a simple example to swap the contents of two multimaps:
#include<iostream>
#include<map>
using namespace std;
int main()
{
// Take any two multimaps
multimap<int, char> multimap1, multimap2;
multimap1 = {
{1, 'a'},
{2, 'b'},
{2, 'c'},
{4, 'd'} };
multimap2 = {
{1, 'x'},
{2, 'y'},
{2, 'z'} };
// Swap elements of multimaps
swap(multimap1, multimap2);
// Print the elements of multimaps
cout << "multimap1:\n"<< "\tKEY\tELEMENT\n";
for (auto it = multimap1.begin();
it != multimap1.end(); it++)
cout << "\t" << it->first << "\t" << it->second << '\n';
cout << "multimap2:\n"<< "\tKEY\tELEMENT\n";
for (auto it = multimap2.begin();
it != multimap2.end(); it++)
cout << "\t" << it->first << "\t" << it->second << '\n';
return 0;
}
Output:
multimap1:
KEY ELEMENT
1 x
2 y
2 z
multimap2:
KEY ELEMENT
1 a
2 b
2 c
4 d
In the above example, another form of swap function is used to swap the contents of two multimaps.
Example 4
Let's see a simple example:
#include <iostream>
#include <string>
#include <map>
using namespace std;
void show(const char *msg, multimap<string, int> mp);
int main() {
multimap<string, int> m1, m2;
m1.insert(pair<string, int>("A", 100));
m1.insert(pair<string, int>("B", 300));
m1.insert(pair<string, int>("B", 200));
// Exchange the contents of m1 and m2.
cout << "Exchange m1 and m2.\n";
m1.swap(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 multimap<string, int> by using an iterator.
void show(const char *msg, multimap<string, int> mp) {
multimap<string, int>::iterator itr;
cout << msg << endl;
for(itr=mp.begin(); itr != mp.end(); ++itr)
cout << " " << itr->first << ", " << itr->second << endl;
cout << endl;
}
Output:
Exchange m1 and m2.
Contents of m2:
A, 100
B, 300
B, 200
Contents of m1:
m1 is now empty.
In the above example, contents of multimap m1 are swapped to multimap m2 and after swapping m1 multimap have been cleared.