Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 04 Sep, 2021
Geek Week

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
 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :