以下の関数を呼び出すと、入力がベクターに追加されますが、常に 6 つの余分なセルがベクターに追加されます。なぜそれが起こっているのですか?
これは関連する関数です:
void seperate_words(string str1, vector<struct wordstype> &vec1)
{
string temp_str;
string::iterator is=str1.begin();
wordstype input_word;
while (is<str1.end())
{
if (((*is)!='-')&&((*is)!='.')&&((*is)!=',')&&((*is)!=';')&&((*is)!='?')&&((*is)!='!')&&((*is)!=':'))
{
temp_str.push_back(*is);
++is;
}
else
{
input_word.word=temp_str;
vec1.push_back(input_word);
is=str1.erase(is);
temp_str.clear();
}
}
input_word.word=temp_str;
vec1.push_back(input_word);
temp_str.clear();
}
関数を呼び出すメインプログラムの関連する間隔は次のとおりです。
**while(end_flag==-1){
cin>> temp_string;
end_flag=temp_string.find(end_str);/*indicates whether the end sign is precieved*/
seperate_words(temp_string,words_vecref);/*seperats the input into single words and inserts them into a vector*/
}
int x=words_vec.size();
cout<<x<<" "<<std::endl;
for (vector<struct wordstype>::iterator p_it=words_vec.begin();p_it<words_vec.end();p_it++)
{cout<<(*p_it).word<<" ";}**
例: 私は通りを歩いています。ベクトル サイズを 6 要素だけ (12 要素ではなく) 増やす必要があります。
出力が期待されます: 6 つのセルのみで、それぞれの oe には、エントリの順序で入力から単語が入力されます。