本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
pre_permutation
----------------------------------------------------------------template <class BidirectionalIterator> bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last) { if (first == last) return false; BidirectionalIterator i = first; ++i; if (i == last) return false; i = last; --i; for(;;) { BidirectionalIterator ii = i; --i; if (*ii < *i) { //满足 *ii < *i --> next_permutation 时的条件是 *i < *ii BidirectionalIterator j = last; while (!(*--j < *i)); // 找到满足 *j < *i 的 *j --> next_permutation 时的条件是 *i < *j iter_swap(i, j); reverse(ii, last); return true; } if (i == first) { reverse(first, last); return false; } } }
int main() { int A[] = {2, 3, 4, 5, 6, 1}; const int N = sizeof(A) / sizeof(int); cout << "Initially: "; copy(A, A+N, ostream_iterator<int>(cout, " ")); cout << endl; prev_permutation(A, A+N); cout << "After prev_permutation: "; copy(A, A+N, ostream_iterator<int>(cout, " ")); cout << endl; next_permutation(A, A+N); cout << "After next_permutation: "; copy(A, A+N, ostream_iterator<int>(cout, " ")); cout << endl; }
STL 源码剖析 算法 stl_algo.h -- pre_permutation,布布扣,bubuko.com
STL 源码剖析 算法 stl_algo.h -- pre_permutation
原文:http://blog.csdn.net/zhengsenlie/article/details/37994755