C++ Program to Implement Prev_Permutation in STL

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.

  1. /*
  2.  * C++ Program to Implement Prev_Permutation in Stl
  3.  */
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. void display(int a[], int n)
  8. {
  9.     for(int i = 0; i < n; i++)
  10.     {
  11.         cout<<a[i]<<"  ";
  12.     }
  13.     cout<<endl;
  14. }
  15. int main ()
  16. {
  17.     int num, i;
  18.     cout<<"Enter number of elements to be inserted: ";
  19.     cin>>num;
  20.     int myints[num];
  21.     for (i = 0; i < num; i++)
  22.     {
  23.         cout<<"Enter "<<i + 1<<" element: ";
  24.         cin>>myints[i];
  25.     }
  26.     sort (myints, myints + num);
  27.     reverse (myints, myints + num);
  28.     cout << "The "<<num<<"! possible permutations with ";
  29.     cout<<num<<" elements: "<<endl;
  30.     do
  31.     {
  32.         display(myints, num);
  33.     }
  34.     while (prev_permutation(myints, myints + num));
  35.     return 0;
  36. }

$ 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.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.