写 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;
}
写好了先点「运行」试试示例,没问题再点「提交」。