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

Started by
1 comment, last by Beer Hunter 18 years ago
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
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.
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()));

This topic is closed to new replies.

Advertisement