C++ default value discussion

I recently learnt through reading someone’s solution, that elements in a map which have not been assigned a value yet are directly assigned zero when used with integers, as it is the default value. What other important defaults are there? For example, I know that vectors don’t have such defaults…

Thanks in advance :slight_smile:

1 Like

std::map doesn’t have default values per se.
And there are no elements in a map that have no value assigned.
When you have a map m of type std::map< K,V> and use operator[] with key k, you’re not just asking for the value of the element with key k.

Instead you’re asking:“does this map have a value for key k? if yes return that, else insert a default constructed/initialized element for key k and return that.”

So the value you’ll get returned from a map< K,V> when using operator[] with a key not in the map depends on the default value for the type of V. This is 0, 0l, 0ll, 0.0 and so on the primitive built-in types or whatever the default constructor creates for user defined types.


Thank you! I would upvote if I could!