Kodewick
開始學 →
基礎←→0 / 86

每個詞幾次

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

寫 count_words(words),回傳一個 map:每個詞出現了幾次。

示例
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 也能執行
結果

寫好了先點「執行」試試示例,沒問題再點「提交」。