STL: Algorithm: next_permutation ()



#include <algorithm>
using namespace std;

int main ()
{
    int array [] = {1, 0, 0, 1};
    int array_length = 4;

    sort ( array, array + array_length );
    // make the array sorted

    do {
        printf ("%d %d %d %d \n", array [0], array [1], array [2], array [3]);
    } while ( next_permutation ( array, array + array_length ) );

    return 0;
}

/* output
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0

Process returned 0 (0x0)
execution time : 0.0046 s
Press any key to continue.
*/


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s