- template <class BidirectionIterator>
- bool next_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(*i < *ii) {
- BidirectionIterator j = last;
- while(!(*i < *--j));
- iter_swap(i, j);
- reverse(ii, last);
- return true;
- }
- if(i == first) {
- reverse(first, last);
- return false;
- }
- }
- }
转载于:https://blog.51cto.com/zxn990/1173047