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'吗? –