fork download
  1. #include <iostream> // std::cout
  2. #include <algorithm> // std::next_permutation, std::sort
  3. #include <string> // std::string
  4. #include <vector> // std::vector
  5.  
  6. int main () {
  7. std::string sentence1 = " B Sentence number two ";
  8. std::string sentence2 = " B Sentence number two ";
  9. std::string sentence3 = " C Sentence number three ";
  10. std::string sentence4 = " D Sentence number four ";
  11.  
  12. // Store all the elements in a container ( here a std::vector)
  13. std::vector<std::string> myVectorOfStrings;
  14. // In the vector we add all the sentences.
  15. // Note : It is possible to do myVectorOfStrings.push_back("Some sentence");
  16. myVectorOfStrings.push_back(sentence1);
  17. myVectorOfStrings.push_back(sentence2);
  18. myVectorOfStrings.push_back(sentence3);
  19. myVectorOfStrings.push_back(sentence4);
  20.  
  21. // The elements must be sorted to output all the combinations
  22. std::sort (myVectorOfStrings.begin(),myVectorOfStrings.end());
  23.  
  24.  
  25. std::cout << "The 4! possible permutations with 4 elements:\n";
  26. do {
  27. //This printing can be improved to handle any number of sentences, not only four.
  28. std::cout << myVectorOfStrings[0] << ' ' << myVectorOfStrings[1] << ' ' << myVectorOfStrings[2] << ' ' << myVectorOfStrings[3] << '\n';
  29. } while ( std::next_permutation(myVectorOfStrings.begin(),myVectorOfStrings.end()) );
  30.  
  31. std::cout << "After loop: " << myVectorOfStrings[0] << ' ' << myVectorOfStrings[1] << ' ' << myVectorOfStrings[2] << ' ' << myVectorOfStrings[3] << '\n';
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 3036KB
stdin
Standard input is empty
stdout
The 4! possible permutations with 4 elements:
 B Sentence number two   B Sentence number two   C Sentence number three   D Sentence number four 
 B Sentence number two   B Sentence number two   D Sentence number four   C Sentence number three 
 B Sentence number two   C Sentence number three   B Sentence number two   D Sentence number four 
 B Sentence number two   C Sentence number three   D Sentence number four   B Sentence number two 
 B Sentence number two   D Sentence number four   B Sentence number two   C Sentence number three 
 B Sentence number two   D Sentence number four   C Sentence number three   B Sentence number two 
 C Sentence number three   B Sentence number two   B Sentence number two   D Sentence number four 
 C Sentence number three   B Sentence number two   D Sentence number four   B Sentence number two 
 C Sentence number three   D Sentence number four   B Sentence number two   B Sentence number two 
 D Sentence number four   B Sentence number two   B Sentence number two   C Sentence number three 
 D Sentence number four   B Sentence number two   C Sentence number three   B Sentence number two 
 D Sentence number four   C Sentence number three   B Sentence number two   B Sentence number two 
After loop:  B Sentence number two   B Sentence number two   C Sentence number three   D Sentence number four