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 comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.