Home » C++ algorithm is_partitioned() function

C++ algorithm is_partitioned() function

by Online Tutorials Library

C++ Algorithm is_partitioned()

C++ Algorithm is_partitioned() is used to test to see if a range [first, last) is partitioned according to a predicate. In other words, all the elements in the range that satisfies the predicate are at the beginning of the sequence.

If the range is empty then it returns true.

Syntax

Parameter

first: An input iterator pointing to the first element in the range.

last: An input iterator pointing to the past last element in the range.

pred: A user-defined unary predicate function that returns true for the elements expected to be found in the beginning of the range.

Return value

This function returns true if the range is empty or is partitioned by given predicate pred, otherwise it returns false.

Complexity

Complexity is linear in the range [first, last): calls pred for each element until a mismatch is found.

Data races

The object in the range [first, last) are accessed.

Each element is accessed exactly once.

Exceptions

This function throws an exception if either pred or an operation on iterator throws an exception.

Please note that invalid parameters cause an undefined behavior.

Example 1

Let’s see the simple example to demonstrate the use of is_partitioned():

Output:

Before Partition: 1 2 3 4 5   After partition : 4 2 3 1 5     Is it partitioned?  Yes,It is Partitioned  

Example 2

Let’s see another simple example:

Output:

All the even no. are present before odd no.  All the even no. are not present before odd no.  

Example 3

Let’s see another simple example:

Output:

false true false   

Example 4

Let’s see another simple example:

Output:

Before Partition: 11 2 3 4 15 12   After partition: 4 2 3 11 15 12     Is it partitioned?  Yes, It is Partitioned.  

Next TopicC++ Algorithm

You may also like