寫 count_words(words),回傳一個 map:每個詞出現了幾次。
count_words({"cat", "dog", "cat"})→{'cat': 2, 'dog': 1}count_words({})→{}map<string, int> count_words(const vector<string> &words)#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;
}
寫好了先點「執行」試試示例,沒問題再點「提交」。