Standard C++ Generic Algorithm 'next_permutation()'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whodgson
    Contributor
    • Jan 2007
    • 542

    Standard C++ Generic Algorithm 'next_permutation()'

    The example of this algorithm is taken from the book I'm learning from.
    It compiles but won't run. Can anyone see what's wrong?
    Code:
    #include<iostream>
    #include<algorithm>
    //have also included <cmath> and <cstdlib> to no affect.
    using namespace std;
    
    int main()
    {
        char* s="ABCD";
        for(int i=0;i<24;i++)
        { next_permutation(s,s+4);
        cout<<(i%8?'\t':'\n')<<s;
        }
    system("pause");
    return 0;
    
    }
    //output is meant to be:-
    ABDC ACBD ACDB etc ..to ABCD ie(.until all 24 combinations
    are printed.)
    Appreciate any thoughts.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Define, "won't run".

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      "abcd" is constant ( eg a string within executable code ) and is not supposed to be changed. Copy it to some other string beforehand.

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        oler1s
        'won't run' means (loosely) that when the IDE Bloodshed Dev C++(v 4.9.9.2) runs the .exe file the monitor screen changes to a black DOS screen and after a few second back to the IDE screen whereupon a Windows message box appears which says:
        "next_permutati on.exe has encountered a problem and needs to close. We are sorry for the inconvenience." It offers to tell Microsft about the problem.
        newb16
        I will grapple with your solution.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          Thanks -- this is how it ended up
          //30/09/08 17:03
          Code:
          #include<iostream>
          #include<algorithm>
          using namespace std;
          
          int main()
          {char A,B,C,D;
              char s[4]={'A','B','C','D'};
               for(int i=0;i<24;i++)
               {next_permutation(s,s+4);
               cout<<s[0]<<s[1]<<s[2]<<s[3]<<" ";
               if(i==7||i==15)cout<<"\n";
               }
          cout<<endl;
          system("pause");
          return 0;
          }/*
          output:
          ABDC ACBD ACDB ADBC ADCB BACD BADC BCAD
          BCDA BDAC BDCA CABD CADB CBAD CBDA CDAB
          CDBA DABC DACB DBAC DBCA DCAB DCBA ABCD*/

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            You can also write line #7 like this:

            Code:
            char s[4]= "ABCD";
            It does exactly the same but is a bit more convenient to write.

            kind regards,

            Jos

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              Thanks Jos and Newb16....i get the idea.

              Comment

              • bwagner
                New Member
                • Nov 2009
                • 1

                #8
                comment to JosAH's reply
                You can also write line #7 like this ...
                ... or even more convenient (avoiding redundancy):
                Code:
                char s[]= "ABCD";

                Comment

                Working...