0
我正在打印重複項,並希望擺脫閱讀代碼,直到文檔結束並刪除多餘的空格。從文件中讀取句子並將它們分成兩個單詞短語。
我用這個代碼,每一個句子分成詞。
vector <string> oneWordPhrase;
vector <string> twoWordPhrase;
vector<string>::iterator it1;
vector<string>::iterator it2;
string split = str;
string word;
stringstream stream(split);
while(getline(stream, word, ' '))
{
cout<<word<<endl;
oneWordPhrase.push_back(word);
}//split the sentence into words
for(it1=oneWordPhrase.begin(); it1!=oneWordPhrase.end(); it1++) /* the problem
{ is here. */
if(it1+1 == oneWordPhrase.end())
break; //signal break if we are near the end of a sentence
twoWordPhrase.push_back(*it1 + ' ' + *(it1+1));
}
for(int i=0; i<twoWordPhrase.size(); i++)
cout<<twoWordPhrase[i]<<endl
此代碼適用於一個句子。例如,如果我的字符串是「你好,我的名字是鮑勃,我是一名學生。」我想打印
「你好我」
「我的名字」
「的名字是」
「是Bob」
/*新的句子*/
「我是」
「是一個」
「學生」
但是我的輸出是
「你好,我」
「我的名字」
「的名字是」
「是Bob」
/*問題在這裏。它可以追溯到句子的開頭*/
「你好,我」
「我的名字」
「的名字是」
「是Bob」
「鮑勃·我「
/*它也不承認新句子*/
」我在」
‘是一個’
‘學生’
有我用我的迭代器指向它離開而不是開頭,其中的一種方式。此代碼工作正常的一句話,但它擁有超過1串
難道你不能只使用'continue'而不是'break'嗎? –