---------------------------------------------------
| Date: 2003-05-29 00:05:00 |
| Filename: permutations.cpp |
| Author: detour@metalshell.com |
| |
| http://www.metalshell.com/ |
---------------------------------------------------
/* permutation.cpp by detour@metalshell.com
*
* Example on using next_permitation from the stl
* algorithm library. It will rearrange a string or
* integer into every unique lexical order.
*
* http://www.metalshell.com/
*
*/
#include
#include
#include
using namespace std;
int main() {
vector v_int;
char str[] = "mobscene";
v_int.push_back(1);
v_int.push_back(4);
v_int.push_back(2);
v_int.push_back(0);
// Print each unique order of "mobscene"
while(next_permutation(str, str+strlen(str)))
cout << str << "\t";
cout << "\n\n";
/* when calling next_permutation you must sort the vector
values into ascending order for it work. */
sort(v_int.begin(), v_int.end());
/* Use a do/while loop instead of the normal while loop becuase
the first entry will be skipped if you call next_permutation
first */
int cnt = 0;
do {
// There should be n! (4*3*2*1) entries.
cout << ++cnt << ". ";
// Print out the vector.
for(unsigned int x = 0; x < v_int.size(); x++)
cout << v_int[x];
cout << endl;
} while(next_permutation(v_int.begin(), v_int.end()));
return 0;
}