tags:

views:

113

answers:

2

Hello. I have this :

std::map<int,int> mapy;
++mapy[5];

Is it safe to assume that mapy[5] will always be 1? I mean, will mapy[5] always get the default value of 0 before '++', even if not explicitly declared, as in my code?

Cheers

+4  A: 

Yes, the default value will be the default of that type. If you want another default, you can create a class that behaves like an int but has a different default constructor.

Rasmus Kaj
+6  A: 

As soon as you access the map with the [] operator, if the key doesn't exist it gets added. The default initializer of the int type gets invoked - so it will get a value of 0.

rep_movsd