partition
partition
Category: algorithms
Component type: function
Prototype
template <class ForwardIterator, class Predicate>
ForwardIterator partition(ForwardIterator first,
ForwardIterator last, Predicate pred)
Description
Partition reorders the elements in the range [first, last) based
on the function object pred, such that
the elements that satisfy pred precede the elements
that fail to satisfy it. The postcondition is that, for some
iterator middle in the range [first, last),
pred(*i) is true for every iterator i in the range [first, middle) and
false for every iterator i in the range [middle, last). [1]
The return value of partition is middle.
Definition
Defined in the standard header algorithm, and in the nonstandard
backward-compatibility header algo.h.
Requirements on types
ForwardIterator is a model of Forward Iterator.
Predicate is a model of Predicate.
ForwardIterator's value type is convertible to Predicate's
argument type.
Preconditions
[first, last) is a valid range.
Complexity
Linear. Exactly last - first applications of pred, and at most
(last - first)/2 swaps.
Example
Reorder a sequence so that even numbers precede odd numbers.
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". [1]
Notes
[1]
The relative order of elements in these two blocks is not necessarily
the same as it was in the original sequence. A different algorithm,
stable_partition, does guarantee to preserve the relative order.
See also
stable_partition, Predicate, function object
Copyright ©
1999 Silicon Graphics, Inc. All Rights Reserved.
TrademarkInformation
Wyszukiwarka
Podobne podstrony:
2007 07 Partition Tricks Backing Up Partitions with Partimage2003 02 Fstab Key to Information on Partitions VolumesPartition Manager 12 Virtual Server (Bundle Partition Manager 12 server Connect VD)Shore Howard The Lord of the Rings (Partitura)partition 5 vmsq7luawhlzirjnnvaowhzg6empxwfdau4a7ki vmsq7luawhlzirjnnvaowhzg6empxwfdau4a7kiPartita in a minorEASEUS Partition Master Home 4 1 1partition 1 tg4ild7abxjkk3bd5zokxoxybbrul4chf6v6b6i tg4ild7abxjkk3bd5zokxoxybbrul4chf6v6b6itable? partition intelStrauss J Pizzicato Polka (Partitura) streich quintetAcronis PartitionExpertopis EASEUS Partition Master Professional EditionDebussy Des pas sur la neige [fl ob cl cr fg] partiture & partsSWAP partition pl (2)intel partition tablereparation d une partition ext2 ext3Multi Partition USBwięcej podobnych podstron