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

advertisement
$ 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
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn