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

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 partioned 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

Sequence is not partitioned

Program-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.