Kodewick
Start learning →
Basic←0 / 3

Word count

#003BasicMapsvector5 min

Write count_words(words) to return a map of how many times each word appears.

Examples
count_words({"cat", "dog", "cat"})→{'cat': 2, 'dog': 1}
count_words({})→{}
The functionmap<string, int> count_words(const vector<string> &words)
main.cpp
#include <map>
#include <string>
#include <vector>
using namespace std;

map<string, int> count_words(const vector<string> &words) {
    map<string, int> seen;
    for (const string &w : words) {
        // your code here: add 1 to this word's count
    }
    return seen;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.