C++统计各个单词出现的次数

来源:百度知道 编辑:UC知道 时间:2024/09/20 04:39:56
从屏幕输入一段英文文本,统计各个单词出现的次数,然后按照单词出现频率降序输出单词及其出现的次数。

例如输入:
One little, two little, three little rabbits.

输出:
little 3
one 1
two 1
…….
急求,在线等!
分可以再加,20够不?
要按照单词出现频率降序输出
标点没去掉啊

//不好意思,这个程序没有考虑标点,要考虑的话又要有大量的代码了。。
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include <algorithm>
#include <functional>
using namespace std;
class wordcount
{
public:
string word;
int times;
wordcount(string s,int t)
{
word=s;
times=t;
}
int operator < (wordcount &w){return times<w.times;}
int operator > (wordcount &w){return times>w.times;}
};
int UDgreater ( wordcount elem1, wordcount elem2 )
{
return elem1 > elem2;
}
int main()
{
string s;
map<string,int> word;
vector<wordcount> word_count;
while(cin>>s)//ctrl+z结束
word[s]++;
for(map<string,int>::iterator itr=word.begin();itr!=word.end();itr++)
{
wordcount w(itr->first,itr->second);
word_count.push_back(w);
}