C++20 sections · 1024 units
Open in Course

next_permutation

All arrangements

I'll show you next_permutation(v.begin(), v.end()) to generate the next lexicographical arrangement. It returns false when you reach the last permutation. You'll typically use this in a loop: while (next_permutation(v.begin(), v.end())) to try all arrangements.

Start with a sorted vector. For nn elements, there are n!n! permutations. This gets slow quickly: 10 elements means 3.6 million permutations.