C++ Program to Implement Next_Permutation in STL

This C++ Program demonstrates implementation of Next_Permutation in STL.

Here is source code of the C++ Program to demonstrate Next_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 Next_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.     cout << "The "<<num<<"! possible permutations with ";
  28.     cout<<num<<" elements: "<<endl;
  29.     do
  30.     {
  31.         display(myints, num);
  32.     }
  33.     while (next_permutation(myints, myints + num));
  34.     return 0;
  35. }

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

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.