Search code examples
c++data-structureshashtable

Insertion into map in c++


So, I was going through code on geeksforgeeks for checking which array is lesser of two given arrays. I came across this code snippet and is confused about the usage of the maps.

   map<int, int> mp1; 

   for (int i = 0; i < m; i++) 
       mp1[a[i]]++; 

Here a[i] is the array given and m is the size of the array.

So, Here how they have used map to insert array elements and if so what would be the key values.


Solution

  • std::map::operator[] will insert the element if it doesn't already exist.

    See https://en.cppreference.com/w/cpp/container/map/operator_at