Skip to content

Instantly share code, notes, and snippets.

@ygabo
Created August 12, 2013 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ygabo/6209372 to your computer and use it in GitHub Desktop.
Save ygabo/6209372 to your computer and use it in GitHub Desktop.
Generate all the permutations of a string.
#include <iostream>
#include <string>
void perm(std::string x, int last){
if( last == 0 ){
std::cout << x << std::endl;
return;
}
for( int i = 0; i <=last; i++){
std::swap(x[i], x[last]);
perm(x, last-1);
std::swap(x[i], x[last]);
}
}
int main(){
std::string x = "abc";
perm(x, x.length()-1);
std::cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment