Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

partition


Algorithm

Summary

Places all of the entities that satisfy the given predicate before all of the entities that do not.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

Synopsis

#include <algorithm>
template <class BidirectionalIterator, class Predicate>
BidirectionalIterator
partition (BidirectionalIterator first,
           BidirectionalIterator last,
           Predicate pred);

Description

For the range [first, last), the partition algorithm places all elements that satisfy pred before all elements that do not satisfy pred. It returns an iterator that is one past the end of the group of elements that satisfy pred. In other words, partition returns i such that for any iterator j in the range [first, i), pred(*j) == true, and, for any iterator k in the range [i, last), pred(*j) == false.

Note that partition does not necessarily maintain the relative order of the elements that match and elements that do not match the predicate. Use the algorithm stable_partition if relative order is important.

Complexity

The partition algorithm does at most (last - first)/2 swaps, and applies the predicate exactly last - first times.

Example

Program Output

Warnings

If your compiler does not support default template parameters, then you always need to supply the Allocator template argument. For instance, you need to write:

deque<int, allocator<int> >

instead of:

deque<int>

If your compiler does not support namespaces, then you do not need the using declaration for std.

See Also

stable_partition



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998