map::swap function


The map::swap swaps the elements of the two map containers.

void swap(map& x);

Parameters:
x -The other map object whose elements to be exchanged with.

Return type
void

If the get_allocator()==x.get_allocator( ),the swapping of elements happen in constant time.ELse assignment operation and constructor calls is proportional to the number of elements in the currently managed map container which will of course invoke more time consumption.

Link:map::get_allocator function
Link:constant time

Code example

map<int,char> mp{{2, ‘@’} , {7 , ‘&’} , { 9 , ‘(‘}} ,
mp1{{6 , ‘^’} , { 8 , ‘*’}};

mp.swap(mp1);

cout<< mp[‘6’] << ” ” << mp[‘8’] << endl;

cout<<mp1[‘2’] << endl
<< mp1[‘9’] ;

Output

^ *
@
(