#include #include #include using namespace std; void swap(string & v, int i, int j) { char tmp=v[i]; v[i]=v[j]; v[j]=tmp; } void listPermutations(string & vect, int n_fixed) { // lista tutte le stringhe ottenibili da vect permutandone // i simboli dall' (n_fixed)-esimo in poi. // (Ossia tenendo fissi i primi n_fixed simboli). assert( n_fixed >= 0 ); if ( n_fixed >= vect.length() -1 ) cout << vect << endl; else for(int j = n_fixed; j< vect.length(); j++) { swap(vect, n_fixed, j); listPermutations(vect, n_fixed +1); swap(vect, n_fixed, j); } } int main() { string vect = "ABCDE"; // My test: cout << vect[0] << endl; cout << "We will list all permutations of the " << vect.length() << " symbols in the string " << vect << ":" << endl; listPermutations(vect, 0); }