The is_partitioned()
function is present in the < algorithm >
library. This function is used to check whether the elements of the container are partitioned on the basis of the function passed in the argument or not.
bool is_partitioned(Iterator Begin, Iterator End, Pred);
The is_partitioned()
function takes the following parameters:
Begin
: A bidirectional iterator pointing to the first element in the range.End
: A bidirectional iterator pointing to the last element in the range.Pred
: A user-defined function that defines the condition on the basis of which the check for the elements partitioned is performed.The function returns a Boolean value:
True
: If the elements of the container are partitioned on the basis of the function passed in the argument.False
: If the elements of the container are not partitioned on the basis of the function passed in the argument.Let us look at the code snippet below.
#include <iostream>#include <algorithm>using namespace std;bool mul3(int x){return (x % 3);}int main(){int arr[] = { 3, 21, 110, 15, 32, 65, 34, 57 };partition(arr, arr + 8, mul3);if(is_partitioned(arr, arr + 8, mul3))cout<< "Array is partitioned \n";elsecout<< "Array is not partitioned \n";return 0;}
In lines 1 and 2, we imported the required libraries.
In lines 4 to 6, we defined a function to check whether the element in the range is multiple of 3
or not.
In line 8, we made a main
function.
In line 10, we initialized an array.
In line 12, we used the partition()
function to partition the elements which are not multiples of 3
and elements which are multiples of 3
.
In lines 14 to 17, we used an if-else
statement to display the result message of the is_partitioned()
function which has the same function in argument as the partition()
function.
In this way, we can use is_partitioned()
function in C++.