I want to write a program that gets an integer n and prints all the permutations of 1,2,…,n.
I know there is a recursive way to do this and I know the overhead of function calls.
Is there any way to do this without recursion??
I want to write a program that gets an integer n and prints all
Share
Yes, you can do it iteratively with
std::next_permutationfrom<algorithm>as awhilecondition, or depending on how you set up the loop this may be one of the occasions wheredo...whileis more appropriate.