vector <string> oneWordPhrase;
vector <string> twoWordPhrase;
vector <string> threeWordPhrase;
string str="hello my is bob oh hey jay oh";
vector<string>::iterator it1;
vector<string>::iterator it2;
I broke the sentence up in str into individual words and stored them into vectors called oneWordPhrase. So, the vector will have a size of 7
for(it1=oneWordPhrase.begin(); it1!=oneWordPhrase.end(); it1++)
{
if(it1+1 == oneWordPhrase.end())
break; /* if we reach the last element of the vector
get out of loop because we reached the end */
twoWordPhrase.push_back(*it1 + ' ' + *(it1+1));
}//gets each 2 consecutive words in the sentence
cout<<"two word---------------\n";
for(int i=0; i<twoWordPhrase.size(); i++)
cout<<twoWordPhrase[i]<<endl;
This produces the correct output:
hello my
my is
is bob
bob oh
oh hey
hey jay
jay oh
for(int i=0; i<twoWordPhrase.size()-1; i++)
{
it1=twoWordPhrase.begin()+i;
it2=oneWordPhrase.begin()+i+2;
if(it1==twoWordPhrase.end()-1)
break; //signal break but doesnt work
threeWordPhrase.push_back(*it1 + ' ' + *it2);
}
cout<<"three words-----------\n";
for(int i=0; i<threeWordPhrase; i++)
cout<<threeWordPhrase[i]<<endl;
This produces the correct output but there are two lines of white space at the end
hello my is
my is bob
is bob oh
bob oh hey
oh hey jay
//whitespace
//whitespace
I also tried to use the iterator in my for loop to signal the break but it didn't work. Why is it printing two extra lines of white space?