本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
partition
------------------------------------------------------------------------//所有被 pred 判定为 true 的元素,都被放到前段 //被 pred 判定为 false 的元素,都被放到后段 //不保证保留相对位置 template <class BidirectionalIterator, class Predicate> BidirectionalIterator partition(BidirectionalIterator first, BidirectionalIterator last, Predicate pred) { while (true) { while (true) if (first == last) return first; else if (pred(*first)) ++first; else break; --last; while (true) if (first == last) return first; else if (!pred(*last)) --last; else break; iter_swap(first, last); ++first; } }
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; const int N = sizeof(A)/sizeof(int); partition(A, A + N, compose1(bind2nd(equal_to<int>(), 0), bind2nd(modulus<int>(), 2))); copy(A, A + N, ostream_iterator<int>(cout, " ")); // The output is "10 2 8 4 6 5 7 3 9 1".
STL 源码剖析 算法 stl_algo.h -- partition,布布扣,bubuko.com
STL 源码剖析 算法 stl_algo.h -- partition
原文:http://blog.csdn.net/zhengsenlie/article/details/37932305