map default values

后端 未结 4 1088
时光取名叫无心
时光取名叫无心 2021-01-30 19:22
std::map mapy;
++mapy[5];

Is it safe to assume that mapy[5] will always be 1? I mean, will mapy[5] always get

4条回答
  •  旧时难觅i
    2021-01-30 20:15

    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.

提交回复
热议问题