Kodewick
开始学 →
基础←0 / 3

每个词几次

#003基础mapvector约 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 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。