我需要从容器中打印出前10个最重复的单词。我的程序正在打印所有重复的单词,当我放count=10
时,它会打印前10个重复的单词。但是,我需要打印前10个最重复的单词。我需要使用什么代码才能获得最重复的前10个单词。
必需输出:
Word Count
the 19
a 14
of 11
artificial 11
that 10
to 7
signal 7
and 7
in 6
they 5
我的代码:
// Displays 10 most frequent words in KnownWords
void WordStats::DisplayMostFreqKnownWords(){
int count;
multimap<int,string > displayFreqWords;
multimap<int,string >::reverse_iterator rit = displayFreqWords.rbegin();
for (Paragraph = KnownWords.begin(); Paragraph != KnownWords.end();
++Paragraph)
{
string word = (*Paragraph).first;
int cnt = (*Paragraph).second.size();
displayFreqWords.insert(pair<int,string>(cnt,word));
}
cout <<" Word Count\n";
for(;rit!=displayFreqWords.rend(); count<=10, ++rit , ++count){
string word = (*rit).second;
int cnt = (*rit).first;
cout << setw(15) << word << setw(10) << cnt << endl;
}
}
我的输出:
Word Count
about 1
aggregate 1
another 1
any 2
approach 2
attention 3
biology 3
board 4
common 5
constitute 5