Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

nth permutation c++ stl

// C++ program to print nth permutation with
// using next_permute()
#include <bits/stdc++.h>
using namespace std;
  
// Function to print nth permutation
// using next_permute()
void nPermute(string str, long int n)
{
    // Sort the string in lexicographically
    // ascending order
    sort(str.begin(), str.end());
  
    // Keep iterating until
    // we reach nth position
    long int i = 1;
    do {
        // check for nth iteration
        if (i == n)
            break;
  
        i++;
    } while (next_permutation(str.begin(), str.end()));
  
    // print string after nth iteration
    cout << str;
}
  
// Driver code
int main()
{
    string str = "GEEKSFORGEEKS";
    long int n = 100;
    nPermute(str, n);
    return 0;
}
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #nth #permutation #stl
ADD COMMENT
Topic
Name
4+9 =