52. Generating all the permutations of a string

You can solve this problem by taking advantage of some general-purpose algorithms from the standard library. The simplest of the two required versions is the non-recursive one, at least when you use std::next_permutation(). This function transforms the input range (that is required to be sorted) into the next permutation from the set of all possible permutations, ordered lexicographically with operator< or the specified comparison function object. If such a permutation exists then it returns true, otherwise, it transforms the range into the first permutation and returns false. Therefore, a non-recursive implementation based on std::next_permuation() looks like this:

void print_permutations(std::string ...

Get The Modern C++ Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.