|
-
January 9th, 2011, 10:55 AM
#1
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.
-
January 9th, 2011, 01:41 PM
#2
Re: How to generate a List of ordering of 4! in C++
 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
Last edited by Paul McKenzie; January 9th, 2011 at 06:44 PM.
-
January 9th, 2011, 05:54 PM
#3
Re: How to generate a List of ordering of 4! in C++
Thanks a lot Paul, Best to you!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|