multimap::swap() is used to swap the contents of one multimap with another multimap of same type and size.
Syntax:-
multimap1.swap(multimap2)
Parameters :
The name of the multimap with which
the contents have to be swapped.
Result :
All the elements of the 2 multimap are swapped.
Examples:
Input: multimap1 = { ('a',1), ('b',2), ('c',3)
multimap2 = ( ('d',4), ('e',5) )
multimap1.swap(multimap2);
Output: MultiMap 1 data
('d', 4), ('e', 5)
MultiMap 2 data
('a',1), ('b',2), ('c',3)
Input: multimap1 = { ('abc',10) , ('bef',12) , ('efg',13)
multimap2 = ( ('def',14), ('ehi',15) )
multimap1.swap(multimap2);
Output: multimap 1 data
('def',14), ('ehi',15)
multimap 2 data
('abc',10) , ('bef',12) , ('efg',13)
#include<iostream>
#include<map>
using namespace std;
int main()
{
multimap< char , int > m1;
multimap< char , int > m2;
multimap< char , int >:: iterator iter;
m1.insert(make_pair( 'a' ,1));
m1.insert(make_pair( 'b' ,2));
m1.insert(make_pair( 'c' ,3));
m2.insert(make_pair( 'd' ,4));
m2.insert(make_pair( 'e' ,5));
m1.swap(m2);
cout << "MultiMap 1 data" << "\n" ;
for ( iter = m1.begin() ;
iter != m1.end() ; iter++)
cout << (*iter).first << " "
<< (*iter).second << "\n" ;
cout << "MultiMap 2 data" << "\n" ;
for ( iter = m2.begin() ;
iter != m2.end() ; iter++ )
cout << (*iter).first << " "
<< (*iter).second << "\n" ;
}
|
Output:-
MultiMap 1 data
d 4
e 5
MultiMap 2 data
a 1
b 2
c 3