Share via


next_permutation

next_permutation

template<class BidIt>
    bool next_permutation(BidIt first, BidIt last);
template<class BidIt, class Pred>
    bool next_permutation(BidIt first, BidIt last, Pred pr);

The first template function determines a repeating sequence of permutations, whose initial permutation occurs when the sequence designated by iterators in the range [first, last) is ordered byoperator<. (The elements are sorted in ascending order.) It then reorders the elements in the sequence, by evaluating swap(X, Y) for the elements X and Y zero or more times, to form the next permutation. The function returns true only if the resulting sequence is not the initial permutation. Otherwise, the resultant sequence is the one next larger lexicographically than the original sequence. No two elements may have equivalent ordering.

The function evaluates swap(X, Y) at most (last - first) / 2 times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pr(X, Y).

Sample programs: next_permutation and next_permutation (predicate version).