This C++ Program demonstrates implementation of Prev_Permutation in STL.
Here is source code of the C++ Program to demonstrate Prev_Permutation in STL. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Implement Prev_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);
reverse (myints, myints + num);
cout << "The "<<num<<"! possible permutations with ";
cout<<num<<" elements: "<<endl;
do
{
display(myints, num);
}
while (prev_permutation(myints, myints + num));
return 0;
}
$ g++ Prev_Permutation.cpp $ a.out Enter number of elements to be inserted: 3 Enter 1 element: 3 Enter 2 element: 2 Enter 3 element: 1 The 3! possible permutations with 3 elements: 3 2 1 3 1 2 2 3 1 2 1 3 1 3 2 1 2 3 ------------------ (program exited with code: 0) Press return to continue
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in Data Structure I
- Participate in Data Structure I Certification Contest
- Become a Top Ranker in Data Structure I
- Take Data Structure I Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy Data Structure Books
- Apply for Data Structure Internship
- Practice Computer Science MCQs
- Buy Programming Books
- Buy Computer Science Books