fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. int main() {
  8. vector <string> oneWordPhrase = {"hello", "my", "is", "bob", "oh", "hey", "jay", "oh"};
  9. vector <string> twoWordPhrase;
  10. vector <string> threeWordPhrase;
  11.  
  12. vector<string>::iterator it1;
  13. vector<string>::iterator it2;
  14. for(it1=oneWordPhrase.begin(); it1!=oneWordPhrase.end(); it1++)
  15. {
  16. if(it1+1 == oneWordPhrase.end())
  17. break; /* if we reach the last element of the vector
  18. get out of loop because we reached the end */
  19. twoWordPhrase.push_back(*it1 + ' ' + *(it1+1));
  20. }//gets each 2 consecutive words in the sentence
  21.  
  22. cout<<"two word---------------\n";
  23. for(int i=0; i<twoWordPhrase.size(); i++)
  24. cout<<'[' << twoWordPhrase[i] << ']' <<endl;
  25.  
  26. for(int i=0; i<twoWordPhrase.size()-1; i++)
  27. {
  28. it1=twoWordPhrase.begin()+i;
  29. it2=oneWordPhrase.begin()+i+2;
  30. if(it1==twoWordPhrase.end()-2)
  31. break; //signal break but doesnt work
  32. threeWordPhrase.push_back(*it1 + ' ' + *it2);
  33. }
  34. cout<<"3 word---------------\n";
  35. for(int i=0; i<threeWordPhrase.size(); i++)
  36. cout<<'[' << threeWordPhrase[i] << ']' <<endl;
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
two word---------------
[hello my]
[my is]
[is bob]
[bob oh]
[oh hey]
[hey jay]
[jay oh]
3 word---------------
[hello my is]
[my is bob]
[is bob oh]
[bob oh hey]
[oh hey jay]