标签:style blog http color os io
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
next_permutation
-----------------------------------------------------------------------template <class BidirectionalIterator> bool next_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 (*i < *ii) { //满足 *i < *ii BidirectionalIterator j = last; while (!(*i < *--j)); // 找到满足 *i < *j 的 *j iter_swap(i, j); reverse(ii, last); //将 ii右端的所有元素颠倒 return true; } if (i == first) { reverse(first, last);//已经是最后一个排列了,逆向重排回到第一个排列 return false; } } }
int main() { int a[] = {0,1,2,3,4}; int n = sizeof(a)/sizeof(int); do{ copy(a, a + n, ostream_iterator<int>(cout, " ")); cout << endl; }while(next_permutation(a, a + n)); }
STL 源码剖析 算法 stl_algo.h -- next_permutation,布布扣,bubuko.com
STL 源码剖析 算法 stl_algo.h -- next_permutation
标签:style blog http color os io
原文地址:http://blog.csdn.net/zhengsenlie/article/details/37967657