I am 90% sure that it’s because using map[x] actually creates an instance of x in map and adds it to the list to be iterated.On the other hand map.find() doesn’t create any such instance and just checks the presence of the element in the map.So map size remains the same if we use find function.
I thought of using unordered_map instead of a map and it is showing some error which I am not able to understand
Which STL function available in maps is not available in unordered_map?
You have to define a custom hash function, if you want to use non standard data types in unordered_map. The Hash function for int is predefined, but not for stl. For a map, a hash function is not needed but only a < operator should be defined, which it is for stl. You may check this link to learn more about it.