The std::next_permutation algorithm is a bit weird to use. This is because it accepts only a begin/end pair of iterators and then returns true if it is able to find the next permutation. Otherwise, it returns false. But what does the next permutation even mean?
The algorithm with which std::next_permutation finds the next lexicographical order of the items, works as follows:
- Find the largest index i such that v[i - 1] < v[i]. If there is none, then return false.
- Now, find the largest index j such that j >= i and v[j] > v[i - 1].
- Swap the items at position j and position i - 1.
- Reverse the order of the items from position i to the end of the range.
- Return true.
The individually permuted orders we get out of this will always appear in the same sequence. In order to see all the possible permutations, we sorted the array first, because if we entered "c b a", for example, the algorithm would terminate immediately, as this already is the last lexicographic order of the elements.