Q:

Assuming word_count is a map from string to size_t and word is a string, explain the following loop

0

Assuming word_count is a map from string to size_t and word is a string, explain the following loop:

while (cin >> word) ++word_count.insert({word, 0}).first->second;

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

while (cin >> word)
      ++word_count.insert({word, 0}).first->second

The code just does the same thing as `++word_count[word];`. If the `word` is in `map`, then increase the count of that word by 1, else insert that word into `map`, set the count to 0, and increase the count by 1.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now