K12教育赛事综合服务平台
聚乐之家官方网站
下载聚乐之家官方App
专注青少年竞赛题库网站
假设单词均为合法小写英文字符串,无需处理特殊字符,使用STL默认提供的容器实现即可。
选用std::map<std::string, int>,遍历文章逐个读取单词,将对应键的值自增1完成计数
std::map<std::string, int>
选用std::unordered_map<std::string, int>,遍历文章逐个读取单词,将对应键的值自增1完成计数
std::unordered_map<std::string, int>
选用std::set<std::pair<std::string, int>>,每次读取单词后先查找是否存在,存在则删除旧记录后插入计数加1的新记录
std::set<std::pair<std::string, int>>
选用std::vector<std::pair<std::string, int>>,每次读取单词后遍历所有元素查找对应单词,找到则将计数加1,没找到则插入新元素
std::vector<std::pair<std::string, int>>