I am wondering if this is a standard that calling [] without assignment will initialize the value of this key-value pair to be 0, if value is of type int?
// the way I did before, can I skip the if() checking?
std::unordered_map<std::string, size_t> word_map;
for (const auto &w : { "this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"}) {
if (!word_map.count(w)) {
word_map[w] = 0;
} else {
++word_map[w];
}
}
Can i do the following instead:
std::unordered_map<std::string, size_t> word_map;
for (const auto &w : { "this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"}) {
++word_map[w];
}
Thanks.
/*-------------
---------------
-------------*/
UPDATES:
I just checked out this page about en.cppreference.com/w/cpp/language/default_initialization it seems that if we reply on the default initialization int x; the value of x will be indeterminate. So I guess word_map[w] does not necessarily == 0 when first called?