我必须阅读dictionary.txt
和test.txt
。然后,必须检查test.txt
中的每个单词,如果它相等,那么这个单词将被添加到KnownWords
容器中,剩下的将在UnknownWords
容器中。我到目前为止所做的一切。但是,在功能DisplayKnownWordStats()
我也必须显示单词的位置。我尝试了以下代码,但它没有给我正确的位置值。
有人可以帮助我从KnownWords
获得test.txt
的正确位置。
void WordStats::ReadTxtFile()
{
std::ifstream ifile(Filename);
if (!ifile)
{
std::cerr << "Error Opening file " << Filename << std::endl;
exit(1);
}
for (std::string word; ifile >> word; )
{
transform(word.begin(), word.end(), word.begin(), ::tolower);
WordMap & Words = (Dictionary.count(word) ? KnownWords : UnknownWords);
Words[word].push_back(ifile.tellg());
}
std::cout << KnownWords.size() << " known words read." << std::endl;
std::cout << UnknownWords.size() << " unknown words read." << std::endl;
}
// Displays stats of words in KnownWords
void WordStats::DisplayKnownWordStats()
{
cout << " ";
cout << " Word Count Position(s)\n";
for (Paragraph = KnownWords.begin(); Paragraph != KnownWords.end(); ++Paragraph)
{
string word = (*Paragraph).first;
vector<int> vect = (*Paragraph).second;
int cnt = vect.size();
cout << setw(15) << word << setw(10) << cnt << " ";
for (int i = 0; i<cnt; i++)
cout << vect[i] << ' '; // something wrong with this code
cout << endl;
}
}
void WordStats::ReadTxtFile(){
int pos=-1;
string word;
std::ifstream ifile(Filename);
if(!ifile)
{
std::cerr << "Error Opening file " << Filename << std::endl;
exit(1);
}
for (std::string word; ifile >> word; )
{
transform (word.begin(), word.end(), word.begin(), ::tolower);
word.erase(std::remove_if(word.begin(), word.end(), [](char c)
{
return (c < 'a' || c > 'z') && c != '\'' ;
}), word.end());
if (word == "") continue;
pos++;
if (Dictionary.find(word) != Dictionary.end())
{
Paragraph = KnownWords.find(word); //adding word to the knownWords
if (Paragraph != KnownWords.end())
{
Paragraph->second.push_back(pos); //adding position of the word
}
else
{
KnownWords[word] = vector<int>(1, pos); //adding record which is new
}
}
else
{
Paragraph = UnknownWords.find(word); //adding word to the knownWords
if (Paragraph != UnknownWords.end())
{
Paragraph->second.push_back(pos); //adding position of the word
}
else
{
UnknownWords[word] = vector<int>(1, pos); //adding record which is new
}
}
}
cout << KnownWords.size() << " known words read." << endl;
cout << UnknownWords.size() << " unknown words read." << endl;
}
void WordStats::DisplayKnownWordsStats()
{
vector<int>::iterator v;
cout << "\n";
cout << setw(22) << "Word" << setw(12) << "Count" << setw(20) << "Position(s)" << endl;
for (Paragraph = KnownWords.begin(); Paragraph != KnownWords.end(); Paragraph++)
{
string word = (*Paragraph).first;
vector<int> vect = (*Paragraph).second;
int count = vect.size();
cout << setw(22) << word << setw(9) << count ;
cout << " ";
for ( v = vect.begin(); v < vect.end(); v++)
{
cout << *v << " ";
}
cout << endl;
}
cout << "\n";
}
Words[word].push_back(ifile.tellg());
以上ifile.tellg()
在一个单词后面返回一个位置。例如,如果文件包含单词“word”,则从文件中读取第一个单词,然后ifile.tellg()
返回4.看起来您希望位置0在那里。你应该像下面这样做:
Words[word].push_back(ifile.tellg() - word.size());