Create A histogram using C++ with map/unordered_map: the default value for a non-existant key

后端 未结 3 2050
盖世英雄少女心
盖世英雄少女心 2021-01-20 17:12

I am defining a small function to create a histogram of a vector of integers, Initially I defined the following function that first test whether the key exists in the map be

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-20 18:04

    This is a guarantee by the behavior of std::map's operator [] overload. If the key does not exist then it will be value-initialized for you. Since int, when value-initialized is assigned to zero, you're safe here.

提交回复
热议问题