 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Lexicographically next permutation in C++
Here we will see how to generate lexicographically next permutation of a string in C++. The lexicographically next permutation is basically the greater permutation. For example, the next of “ACB” will be “BAC”. In some cases, the lexicographically next permutation is not present, like “BBB” or “DCBA” etc.
In C++ we can do it by using a library function called next_permutation(). This is present in the algorithm header file.
Example
#include <iostream>
#include <algorithm>
using namespace std;
main() {
   string s = "DBAC";
   for(int i = 0; i<5; i++) {
      bool val = next_permutation(s.begin(), s.end());
      if (val == false) {
         cout << "No next permutation" << endl;
         break;
      } else
      cout << "Next: " << s << endl;
   }
}
Output
Next: DBCA Next: DCAB Next: DCBA No next permutation
Advertisements
                    