increment map<string, int> using ++ operator
C++MapOperatorsC++ Problem Overview
I have a map
map[word]++; //(where map is the name of my map, I'm not using map as a name of course)
so that if the my map already has 'word' as a key, it increments it, otherwise it creates the new key and increments it.
Here's where I am concerned: if I do map[word]++ on a new key (which is unavoidable in the first word read), will my program crash because the int in my map
Thanks
C++ Solutions
Solution 1 - C++
> will my program crash because the int in my map is unitialized?
No; if the element with key word
doesn't exist, the element will be created and value initialized. A value-initialized int
has a value of 0
.