Open In App

boost::algorithm::is_partitioned() in C++ library

Last Updated : 04 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The is_partitioned() function in C++ boost library is found under the header ‘boost/algorithm/cxx11/is_partitioned.hpp’ which tests if the given sequence is partitioned according to the given predicate or not. Partition here means that all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
Syntax
 

bool is_partitioned ( InputIterator first, InputIterator last, Predicate p ) 
or 
bool is_partitioned ( const Range &r, Predicate p )

 
Parameters: The function accepts parameters as described below: 
 

  • first: It specifies the input iterators to the initial positions in a sequence.
  • second: It specifies the input iterators to the final positions in a sequence.
  • p: It specifies the comparison predicate if specified.
  • r: It specifies the given range completely.

Return Value: The function returns true if the complete sequence is sorted according to the given criteria, else it returns false. 
Below is the implementation of the above approach: 
Program-1
 

CPP




// C++ program to implement the
// above mentioned function
 
#include <bits/stdc++.h>
#include <boost/algorithm/cxx11/is_partitioned.hpp>
using namespace std;
 
// Predicate function to check
// if the element is odd or not
bool isOdd(int i)
{
    return i % 2 == 1;
}
// Drivers code
int main()
{
 
    // Declares the sequence with
    int c[] = { 1, 2, 5, 6, 8 };
 
    // Run the function
    bool ans
        = boost::algorithm::is_partitioned(c, isOdd);
 
    // Condition to check
    if (ans == 1)
        cout << "Sequence is partitioned";
    else
        cout << "Sequence is not partitioned";
    return 0;
}


Output: 

Sequence is not partitioned

 

Program-2
 

CPP




// C++ program to implement the
// above mentioned function
 
#include <bits/stdc++.h>
#include <boost/algorithm/cxx11/is_partitioned.hpp>
using namespace std;
 
// Predicate function to check
// if the element is odd or not
bool isEven(int i)
{
    return i % 2 == 0;
}
// Drivers code
int main()
{
 
    // Declares the sequence with
    int c[] = { 4, 2, 5, 6, 8 };
 
    // Run the function
    bool ans
        = boost::algorithm::is_partitioned(c,
                                           c + 2,
                                           isEven);
 
    // Condition to check
    if (ans == 1)
        cout << "Sequence is partitioned";
    else
        cout << "Sequence is not partitioned";
    return 0;
}


Output: 

Sequence is partitioned

 

Reference: https://www.boost.org/doc/libs/1_70_0/libs/algorithm/doc/html/the_boost_algorithm_library/CXX11/is_sorted.html
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads