C++ Program to Implement Next_Permutation in STL
#include <iostream>
#include <algorithm>
using namespace std;
void display(int a[], int n)
{
for(int i = 0; i < n; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
int main ()
{
int num, i;
cout<<"Enter number of elements to be inserted: ";
cin>>num;
int myints[num];
for (i = 0; i < num; i++)
{
cout<<"Enter "<<i + 1<<" element: ";
cin>>myints[i];
}
sort (myints, myints + num);
cout << "The "<<num<<"! possible permutations with ";
cout<<num<<" elements: "<<endl;
do
{
display(myints, num);
}
while (next_permutation(myints, myints + num));
return 0;
}
Output:
$ g++ Next_Permutation.cpp
$ a.out
Enter number of elements to be inserted: 3
Enter 1 element: 1
Enter 2 element: 2
Enter 3 element: 3
The 3! possible permutations with 3 elements:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
