This C++ program demonstrates the next_permutation algorithm. The program first sorts the string and then permutes the entire string. The next_permutation takes iterators to the beginning and end of the string to be permuted and overwrites the value of the string with next lexicographic permutation. The function returns true if next_permutation is available otherwise returns false.
Here is the source code of the C++ program which demonstrates the net_permutation algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to demonstrate the next_permutation() algorithm
*/
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string s;
std::cout << "Enter the string : ";
std::cin >> s;
sort(s.begin(), s.end());
std::cout << "Permutations of " << s << std::endl;
do
{
std::cout << s << "\t";
}
while (next_permutation(s.begin(), s.end()));
return 0;
}
$ a.out Enter the string : ABCD Permutations of ABCD ABCD ABDC ACBD ACDB ADBC ADCB BACD BADC BCAD BCDA BDAC BDCA CABD CADB CBAD CBDA CDAB CDBA DABC DACB DBAC DBCA DCAB DCBA
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Check Programming Books
- Check Computer Science Books
- Practice Computer Science MCQs
- Check C++ Books
- Practice Programming MCQs