map::begin function


The map::begin returns an iterator pointing to the first element in the map container.

iterator begin( );
const_iterator begin( ) const;

First function
Parameters:
none

Return type
iterator -A read and write iterator that points to the first element in a map container.

Second function
Parameters:
none

Return type
iterator – A read only iterator that points to the first element in a map container.

map::begin function

Code example

map<int,string>mp{ {2 , “Two”} , { 9 , “Nine”} , { 4 , “Four”} };

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

cout<< mpIt->first << mpIt->second << endl ;

mpIt->first=8 ; ///error
mpIt->second=”Eight”; //work fine

cout<< mpIt->first << mpIt->second << endl ;

Output

2 Two
2 Eight

We cannot change the first value or key of the pair element.The key is fixed for a map container.Permitting changeability of the key will compromise the position of the element in the container,which is why it is fixed through out the container’s lifetime.


 


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