Kodewick
学びはじめる →
基礎←→0 / 60

単語を数えよう

#003基礎seen[w]++約 5 分

それぞれの単語が何回出てくるかを map にして返す count_words(words) を書きましょう。

例
count_words({"cat", "dog", "cat"})→{'cat': 2, 'dog': 1}
count_words({})→{}
関数map<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) {
        // ここにコードを書く:この単語の回数に 1 を足す
    }
    return seen;
}
Ctrl + Enter でも実行できます
結果

実行すると例で試せます。よさそうなら提出しましょう。