• Advertisement
Sign in to follow this  

How to output every combination from string eg 123, 132, 321, etc

This topic is 4328 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I was trying to work out how to find every combination of characters from a string of n size. eg for a string of n=3: 123 132 213 231 312 321 I thought it would be simple, but im a bit stumped on how to implement it,thx

Share this post


Link to post
Share on other sites
Advertisement
That's not known as a "combination", but a "permutation". Google for "permutation algorithm" and you'll turn up quite a bit of stuff.

Share this post


Link to post
Share on other sites
If you are using C++ and just want a pre-written function, you can use next_permutation from the <algorithm> header.
std::string mystr = "123";
do
{
std::cout << mystr << std::endl;
}
while (std::next_permutation(mystr.begin(), mystr.end()));

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement