How to generate a List of ordering of 4! in C++
Hi All,
I want to write a func to generate all the permutations of L[1,2,3,4], once at a time and output it in an array...
For example, the first time the func called, it outputs a pointer to L[4]={1,2,3,4};
the second time the func called, it outputs a pointer to L[4]=[1,3,2,4];
...
...
the 4! time the func called, it outputs a pointer to L[4]={4,3,2,1];
Could anyone give me an idea like how to write the code generating an array like this?
My guess is to use some kinds of static variable in the function so it could keep its value after one execution of the function and guide the next execution to generate a different ordering.
Thanks in advance.
Re: How to generate a List of ordering of 4! in C++
Quote:
Originally Posted by
ertss
Hi All,
I want to write a func to generate all the permutations of L[1,2,3,4], once at a time and output it in an array...
For example, the first time the func called, it outputs a pointer to L[4]={1,2,3,4};
the second time the func called, it outputs a pointer to L[4]=[1,3,2,4];
...
...
the 4! time the func called, it outputs a pointer to L[4]={4,3,2,1];
Could anyone give me an idea like how to write the code generating an array like this?
My guess is to use some kinds of static variable in the function so it could keep its value after one execution of the function and guide the next execution to generate a different ordering.
Thanks in advance.
Use std::next_permutation. There is no reason (except for someone forcing you) to write a function to generate permutations, as the standard library has next_permutation for this purpose.
Regards,
Paul McKenzie
Re: How to generate a List of ordering of 4! in C++
Thanks a lot Paul, Best to you!