如何固定输出 C++、STL 中文本的位置
How to fix the position for the text in the output C++,STL
找到KnownWords
和UnknownWords
后需要打印DisplayOriginalText()
中的原文。一切都工作得很好,但我的输出有一点错误。编译器将原文的最后一个词放在 OriginalText
的第一个词中。我在输出中给出了一个例子,以使其更清楚。有人可以找出问题所在以及如何纠正吗?
我的输出:
expensive this is my car and this is so much
要求输出:
this is my car and this car is so much expensive
注意:这只是一个例子。原文为一篇500字左右的文章
头文件:
typedef map<string, vector<int> > WordMap;
typedef WordMap::iterator WordMapIter;
class WordStats
{
public:
WordStats();
void ReadDictionary();
void DisplayDictionary();
void ReadTxtFile();
void DisplayKnownWordStats();
void DisplayUnknownWordStats();
void DisplayMostFreqKnownWords();
void DisplayMostFreqUnknownWords();
void DisplayOriginalText();
private:
WordMap KnownWords;
WordMap UnknownWords;
set<string> Allwords;
WordMapIter Paragraph;
set<string> Dictionary;
char Filename[256];
//add your private fns here
string parse_word(string word);
void DisplayWordStats(WordMap &WMap);
};
我的程序:
void WordStats::DisplayOriginalText(){
std::map<int,string> txt_map;
std::map<int,string>::iterator mit;
/// insert all known words
for (WordMapIter it = KnownWords.begin(); it != KnownWords.end(); it++)
for (vector<int>::iterator pos_it = it->second.begin(); pos_it != it-
>second.end(); pos_it++ )
txt_map.insert(std::pair<int,string>(*pos_it, it->first) );
/// insert all unknown words
for (WordMapIter it = UnknownWords.begin(); it != UnknownWords.end();
it++)
for (vector<int>::iterator pos_it = it->second.begin(); pos_it != it-
>second.end(); pos_it++ )
txt_map.insert(std::pair<int,string>(*pos_it, it->first) );
cout << endl << " - Original Text -" << endl << endl;
for (mit = txt_map.begin(); mit != txt_map.end(); ++mit)
cout << mit->second << " ";
cout << endl;
}
void WordStats::DisplayOriginalText(){
std::map<int,string> mtext;
std::map<int,string>::iterator miter;
vector<int>::iterator v_iter;
// inserting known words
for (Paragraph = KnownWords.begin(); Paragraph != KnownWords.end(); Paragraph++)
{
for ( v_iter = Paragraph->second.begin(); v_iter != Paragraph->second.end(); v_iter++ )
{
mtext.insert(std::pair<int,string>(*v_iter, Paragraph->first) );
}
}
/// inserting unknown words
for (Paragraph = UnknownWords.begin(); Paragraph != UnknownWords.end(); Paragraph++)
{
for ( v_iter = Paragraph->second.begin(); v_iter != Paragraph->second.end(); v_iter++ )
{
mtext.insert(std::pair<int,string>(*v_iter, Paragraph->first) );
}
}
cout <<"\n ";
cout << "--- Original Text ---\n\n";
for (miter = mtext.begin(); miter != mtext.end(); ++miter)
{
cout << miter->second << " ";
}
cout << endl;
}
找到KnownWords
和UnknownWords
后需要打印DisplayOriginalText()
中的原文。一切都工作得很好,但我的输出有一点错误。编译器将原文的最后一个词放在 OriginalText
的第一个词中。我在输出中给出了一个例子,以使其更清楚。有人可以找出问题所在以及如何纠正吗?
我的输出:
expensive this is my car and this is so much
要求输出:
this is my car and this car is so much expensive
注意:这只是一个例子。原文为一篇500字左右的文章
头文件:
typedef map<string, vector<int> > WordMap;
typedef WordMap::iterator WordMapIter;
class WordStats
{
public:
WordStats();
void ReadDictionary();
void DisplayDictionary();
void ReadTxtFile();
void DisplayKnownWordStats();
void DisplayUnknownWordStats();
void DisplayMostFreqKnownWords();
void DisplayMostFreqUnknownWords();
void DisplayOriginalText();
private:
WordMap KnownWords;
WordMap UnknownWords;
set<string> Allwords;
WordMapIter Paragraph;
set<string> Dictionary;
char Filename[256];
//add your private fns here
string parse_word(string word);
void DisplayWordStats(WordMap &WMap);
};
我的程序:
void WordStats::DisplayOriginalText(){
std::map<int,string> txt_map;
std::map<int,string>::iterator mit;
/// insert all known words
for (WordMapIter it = KnownWords.begin(); it != KnownWords.end(); it++)
for (vector<int>::iterator pos_it = it->second.begin(); pos_it != it-
>second.end(); pos_it++ )
txt_map.insert(std::pair<int,string>(*pos_it, it->first) );
/// insert all unknown words
for (WordMapIter it = UnknownWords.begin(); it != UnknownWords.end();
it++)
for (vector<int>::iterator pos_it = it->second.begin(); pos_it != it-
>second.end(); pos_it++ )
txt_map.insert(std::pair<int,string>(*pos_it, it->first) );
cout << endl << " - Original Text -" << endl << endl;
for (mit = txt_map.begin(); mit != txt_map.end(); ++mit)
cout << mit->second << " ";
cout << endl;
}
void WordStats::DisplayOriginalText(){
std::map<int,string> mtext;
std::map<int,string>::iterator miter;
vector<int>::iterator v_iter;
// inserting known words
for (Paragraph = KnownWords.begin(); Paragraph != KnownWords.end(); Paragraph++)
{
for ( v_iter = Paragraph->second.begin(); v_iter != Paragraph->second.end(); v_iter++ )
{
mtext.insert(std::pair<int,string>(*v_iter, Paragraph->first) );
}
}
/// inserting unknown words
for (Paragraph = UnknownWords.begin(); Paragraph != UnknownWords.end(); Paragraph++)
{
for ( v_iter = Paragraph->second.begin(); v_iter != Paragraph->second.end(); v_iter++ )
{
mtext.insert(std::pair<int,string>(*v_iter, Paragraph->first) );
}
}
cout <<"\n ";
cout << "--- Original Text ---\n\n";
for (miter = mtext.begin(); miter != mtext.end(); ++miter)
{
cout << miter->second << " ";
}
cout << endl;
}