map::erase function


The map::erase function erase/remove element or elements from the map container.There are four version of erase function in map:

1 iterator erase(const_iterator pos);
2 iterator erase(iterator pos);
3 size_t erase(const key_type& x);
4 iterator erase(const_iterator first, const_iterator last);

Key_type:Type of the key in the pair element.

LinK: Key_type in map

The first and second version is the same except one accept a const_iterator and the other accept iterator type.


iterator erase(const_iterator pos);

This function erase/remove element from the map container.Once the element is erase it cannot be recovered.

Parameters:
pos – A const_iterator type that points to the position of the element that you wan to remove.

Return type
iterator -The returned iterator points to the position next to the erase element.If no elements is erased end() is returned and iterator points to one past the last element in the map container.

Link: map::end function

Note if the element is a pointer that points to another storage,then erasing the pointer does not erase the storage pointed by the pointer.What happens to the storage is the programmer’s responsibility.

Link:map::begin function

Code example

map<int,string>mp{ {4 , “Four”} , {2 , “Two”} , {10 ,”Ten”} , {7 ,”Seven”} };

map<int,string>::iterator mpIt=mp.begin();

mp.erase(mpIt); //removes the first element,calls the second version

for(auto elem:mp)
{
cout<< elem->first << ” ” << elem->second << endl;
}

Output

4 Four
10 Ten

Another program is given to check the return type.

map<int,int> m{ {4 , 8} , { 9 , 18 } , {100 ,200} };

map<int,int>::const_iterator it=m.begin() , it1;

++it;

it1=mp.erase(it); //removes the second element,calls the first version

cout<< it1->first << ” ” << it1->second << endl;

Output

100 200

‘it1’ points to the third element before the element is released.


 


Stop wasting time,earn money($$$) from your website-Join Now!


size_t erase(const key_type& x);

The srd version erase element using the key of the pair element.

Parameters:
x – Key of the element to be removed.

Return type
size_t – The number of element erased.The returned value is either 1 or 0.It cannot be more than 1 because map does not allow duplicate key.

Code example

map<char,int> ob{ {‘C’, 2} , { ‘M’ , 23 } , { ‘B’ , 68 } };

ob.erase(‘C’);

for(auto elem:ob)
{
cout<< elem->first << ” ” << elem-> << endl ;
}

Output

B 68
M 23

Link:C++11 ranged based for loop

Another code example is given below to check the return type.

Code example

map<char,int> ob{ {‘C’, 2} , { ‘M’ , 23 } , { ‘B’ , 68 } };

size_t n=ob.erase(‘C’);

cout<< n << endl ;

n=ob.erase(‘V’);

cout<< n << endl ;

Output

1
0

Since element with key ‘V’ doesn’t exist the function can’t erase the element and so the return value is 0;no element is erased.






iterator erase(const_iterator first, const_iterator last);

This overloaded version erase a range of elements from the map container.

Parameters:
first – The starting range of the elements to be erased.

last -The end of the range.

Return type
iterator -The ‘last’ iterator is returned.

The range of elements erased from the container is of the form [first,last),the element pointed by last iterator is not erased.

Codee example

map<int,char> mIC{ { 23 , ‘*’ } , { 89 , ‘%’ } , { 12, ‘/’ } };

map<int,char>::iterator It=mIC.begin( ) ,
It1=mIC.begin() ; //begin() returns iterator pointing to the first element

++It1 ;
++It1 ;

mIC.erase( It , It1 ); ///erase first and second element

for(auto elem:mIC)
{
cout<< elem-> first << ” ” << elem->second << endl;
}

Output

89 %

Another code example is given below to check the return type.

map<int,char> mIC{ { 23 , ‘*’ } , { 89 , ‘%’ } , { 12, ‘/’ } };

map<int,char>::iterator It=mIC.begin( ) ,
It1=mIC.begin( ) , //begin() returns iterator pointing to the first element
itRet;

++It1 ;
++It1 ;

itRet=mIC.erase( It , It1 ); ///erase first and second element

cout<< itRet->first << ” ” << itRet->second ;

Output

89 %

Link: map::begin function