# Problem in maps

Here is a gfg problem I am not able to understand how elements are getting stored in map

when I print elements of the map I am getting
7 0
3 0
8 4
etc
here 8 is present in the map coz we added them earlier but why are 3 and 7 getting inserted into the map?
any help would be appreciated

In Map even when you try to access the element using subscript ([]) operator its get added in the map with key as element and value as 0

``````   map <int,int> mp ;
mp[3] = 8 ;
if ( mp[7] == 2 ) cout << "Present" ;
``````

In above example i added 3 with value of 8 and its get added to map
but in if condition I am just checking if 7 is present in map with value 2 its got added in map as i tried to access it using subscript operator

and curr map is {{3,8},{7,0}}

To avoid this
you can used count function which do not add any element in map and it returns 0 or 1 if element is present in map or not

``````   map <int,int> mp ;
mp[3] = 8 ;
if ( mp.count(7) ) {
if ( mp[7] == 2 )
cout << "Present" ;
``````

In this case mp.count(7) gives false as 7 is not present in map