- template <class BidirectionIterator>
- bool prev_permutation(BidirectionIterator first, BidirectionIterator last) {
- if(first == last) return false;
- BidirectionIterator i = first;
- ++i;
- if(i == last) return false;
- i = last;
- --i;
- for(;;) {
- BidirectionIterator ii = i;
- --i;
- if(*ii < *i) {
- BidirectionIterator j = last;
- while(!(*--j < *i));
- iter_swap(i, j);
- reverse(ii, last);
- return true;
- }
- if(i == first) {
- reverse(first, last);
- return false;
- }
- }
- }