vector <string> oneWordPhrase;
vector <string> twoWordPhrase;
vector <string> threeWordPhrase;
vector<string>::iterator it1;
vector<string>::iterator it2;
string str="hello my is bob oh hey jay oh";
string split = str;
string word;
stringstream stream(split);
while( getline(stream, word, ' ') )
{
oneWordPhrase.push_back(word);
}//used to split sentence into words
for(it1=oneWordPhrase.begin(); it1!=oneWordPhrase.end(); it1++)
{
if(it1+1 == oneWordPhrase.end())
break;
twoWordPhrase.push_back(*it1 + ' ' + *(it1+1));
}//getting two word phrases
cout<<"two word---------------\n";
for(int i=0; i<twoWordPhrase.size(); i++)
cout<<twoWordPhrase[i]<<endl;
for(it1=twoWordPhrase.begin(); it1!=twoWordPhrase.end(); it1++)
{
it2=oneWordPhrase.begin()+2;
threeWordPhrase.push_back(*it1 + ' ' + *it2);
++it2; /* was hoping that I can get each word after "is" but it
didn't allow me. the problem is here */
}//getting three word phrases
cout<<"three word---------------\n";
for(int i=0; i<twoWordPhrase.size(); i++)
cout<<threeWordPhrase[i]<<endl;
我得到了两个正确的打印,这是
你好我的
我的
是bob
鲍勃哦
哦,嘿嘿 嘿杰伊jay oh
然而,我的threeWordPhrase打印出来
你好我的
我的是
是鲍勃是
鲍勃哦是
哦,嘿是
嘿杰伊是jay oh is
对于threeWordPhrases我希望打印“你好我是”“我的鲍勃”“是bob哦”“鲍勃哦嘿”一直到“嘿杰伊哦”。
我在myWordPhrase.begin()+ 2指出了我的it2,并希望它会像在数组中那样增加1,但事实并非如此。
我评论了给我问题的代码部分
我很确定如果我能找出3个单词短语我可以做4个和5个单词短语,所以对3个单词的任何帮助都会非常感激!
答案 0 :(得分:0)
执行*it2 = oneWordPhrase.begin() + 2
时,它始终会为您提供oneWordPhrase向量中的第3个成员。您可以使用计数器而不是迭代器,因为您需要遍历两个向量:
vector <string> oneWordPhrase;
vector <string> twoWordPhrase;
vector <string> threeWordPhrase;
vector<string>::iterator it1;
vector<string>::iterator it2;
string str="hello my is bob oh hey jay oh";
string split = str;
string word;
stringstream stream(split);
while( getline(stream, word, ' ') )
{
oneWordPhrase.push_back(word);
}//used to split sentence into words
for(it1=oneWordPhrase.begin(); it1!=oneWordPhrase.end(); it1++)
{
if(it1+1 == oneWordPhrase.end())
break;
twoWordPhrase.push_back(*it1 + ' ' + *(it1+1));
}//getting two word phrases
cout<<"two word---------------\n";
for(int i=0; i<twoWordPhrase.size(); i++)
cout<<twoWordPhrase[i]<<endl;
for(int i=0; i!=twoWordPhrase.size() - 2; i++)
{
threeWordPhrase.push_back( twoWordPhrase[i] + ' ' + oneWordPhrase[i + 2] );
/* was hoping that I can get each word after "is" but it
didn't allow me. the problem is here */
}//getting three word phrases
cout<<"three word---------------\n";
for(int i=0; i<twoWordPhrase.size() - 2; i++)
cout<<threeWordPhrase[i]<<endl;