一篇英文文本,要求利用C++中map容器,来实现单词词频的统计 20
展开全部
粗略写了个,应该能跑起来,自己去完善一下
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
map<string, unsigned> wordcounter;
ifstream input("c:\\input.txt");
const unsigned len = 1 << 10;
char line[len] = { 0 };
while (!input.eof())
{
input.getline(line, len, ' ');
auto iter = wordcounter.find(line);
if (iter == wordcounter.end())
wordcounter[line] = 1;
else
iter->second += 1;
}
for_each(wordcounter.begin(), wordcounter.end(), [](pair<string, unsigned> item) {
cout << item.first << ":" << item.second << endl;
});
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询