CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2010
    Posts
    32

    Angry 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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to generate a List of ordering of 4! in C++

    Quote Originally Posted by ertss View Post
    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.

  3. #3
    Join Date
    Dec 2010
    Posts
    32

    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
  •  





Click Here to Expand Forum to Full Width

Featured