我不知道如何编写这个函数“DisplayOriginalText()”
。我的要求是:我必须实现“DisplayOriginalText()”
公共成员函数。为此,请声明一个本地map<int,string>;
容器并执行与DisplayMostFreqKnownWords()
相同的操作,除了我还应迭代KnownWords和UnknownWords容器中的向量,并为所有单词及其位置添加对。这将根据单词位置编号将单词排序为原始文本顺序。输出将是文件中的原始文本。
标题文件:
typedef map<string, vector<int> > WordMap;
typedef WordMap::iterator WordMapIter;
class WordStats
{
public:
void DisplayMostFreqKnownWords();
void DisplayMostFreqUnknownWords();
void DisplayOriginalText();
private:
WordMap KnownWords;
WordMap UnknownWords;
WordMapIter Paragraph;
set<string> Dictionary;
char Filename[256];
};
我的节目:
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(; count<=10 && rit!=displayFreqWords.rend(); rit++, ++count){
string word = (*rit).second;
int cnt = (*rit).first;
cout << setw(15) << word << setw(10) << cnt << endl;
}
}
// Displays 10 most frequent words in UnknownWords
void WordStats::DisplayMostFreqUnknownWords(){
multimap<int,string > displayFreqUnknownWords;
multimap<int,string >::reverse_iterator rrit =
displayFreqUnknownWords.rbegin();
for (Paragraph = UnknownWords.begin(); Paragraph != UnknownWords.end();
++Paragraph){
string word = (*Paragraph).first;
int cnt = (*Paragraph).second.size();
displayFreqUnknownWords.insert(pair<int,string>(cnt,word));
}
cout <<" Word Count\n";
for(int count=0; count<10 && rrit!=displayFreqUnknownWords.rend();
rrit++, ++count){
string wrd = (*rrit).second;
int ccnt = (*rrit).first;
cout << setw(15) << wrd << setw(10) << ccnt << endl;
}
}
// Displays original text from KnownWords & UnknownWords
void WordStats::DisplayOriginalText(){
map<int,string> DisplayOriginalText;
map<int,string> ::iterator it;
for(int count=0; count<10 && it!=displayFreqUnknownWords.rend(); it++,
++count){
string wrd = (*it).second;
int ccnt = (*it).first;
cout << setw(15) << wrd << setw(10) << ccnt << endl;
}