STL: Algorithm: prev_permutation ()



#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

int main ()
{
    vector <int> v;

    v.push_back (2);
    v.push_back (1);
    v.push_back (3);
    v.push_back (1);

    sort ( v.begin (), v.end (), greater <int> () );
    // descending sort, header : functional

    do {
        printf ("%d %d %d %d \n", v [0], v [1], v [2], v [3]);
    } while ( prev_permutation ( v.begin (), v.end () ) );

    return 0;
}

/* output
3 2 1 1
3 1 2 1
3 1 1 2
2 3 1 1
2 1 3 1
2 1 1 3
1 3 2 1
1 3 1 2
1 2 3 1
1 2 1 3
1 1 3 2
1 1 2 3

Process returned 0 (0x0)   
execution time : 0.031 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