Hello, I'm trying to have a program that prints all the possible combinations of some strings, where order matters and so following the formula: C= n!/(n-k)!.
Now, I wrote this code with which I can have all the combinations, but not for "n" different from "k", what instead I'm actually looking for.
Here is the code:
#include <iostream> // std::cout
#include <algorithm> // std::next_permutation, std::sort
#include <string> // std::string
#include <vector> // std::vector
int main () {
std::string sentence1 = " Sentence number one ";
std::string sentence2 = " Sentence number two ";
std::string sentence3 = " Sentence number three ";
std::string sentence4 = " Sentence number four ";
// Store all the elements in a container ( here a std::vector)
std::vector<std::string> myVectorOfStrings;
// In the vector we add all the sentences.
// Note : It is possible to do myVectorOfStrings.push_back("Some sentence");
myVectorOfStrings.push_back(sentence1);
myVectorOfStrings.push_back(sentence2);
myVectorOfStrings.push_back(sentence3);
myVectorOfStrings.push_back(sentence4);
// The elements must be sorted to output all the combinations
std::sort (myVectorOfStrings.begin(),myVectorOfStrings.end());
std::cout << "The 4! possible permutations with 4 elements:\n";
do {
//This printing can be improved to handle any number of sentences, not only four.
std::cout << myVectorOfStrings[0] << ' ' << myVectorOfStrings[1] << ' ' << myVectorOfStrings[2] << ' ' << myVectorOfStrings[3] << '\n';
} while ( std::next_permutation(myVectorOfStrings.begin(),myVectorOfStrings.end()) );
std::cout << "After loop: " << myVectorOfStrings[0] << ' ' << myVectorOfStrings[1] << ' ' << myVectorOfStrings[2] << ' ' << myVectorOfStrings[3] << '\n';
return 0;
}
Sorry if I make this message even longer, but just to clarify, that is an example of what I'm having:
Are you trying to do combinations or permutations? The title and info says combination but in your code it looks like you are trying to get the permutations..
There is a difference fyi. Permutations order DOES matter and combination order does NOT matter.
Yes, sorry. I just read the discussion you posted and I meant permutations then, I didn't know about the difference.
So now I could just use the last code you wrote and replace boolean with string, right? Should I make more changes to make it work properly?
Thanks.
Yeah if you are using permutations. If you are using combinations then go to that link tntxtnt sent or check out his code. Your assignment looks like it wants that even though in your code you are doing permutations.
Sorry giblit,you're right, again...I just feel so stupid. I also said I would have modified the code myself, but I tried and I saw I'm not capable of doing it yet (I'm really new at C++). I got that I should modify some parts of your code and replace them with ones already in mine, but then there are things like "boolalpha" that I never heard about and I don't know how to handle...sorry if I'm annoying you.
Look at the example on how to deal with other types.
There is a wealth of info in the reference section & and the articles, & the tutorial in the documentation - I recommend reading up on it. Especially the reference for all the functions / algorithms / containers you might use.
> I'm trying to have a program that prints all the possible combinations of some strings,
> where order matters and so following the formula: C= n!/(n-k)!.
...
> and I saw I'm not capable of doing it yet (I'm really new at C++).