boost::algorithm::is_partitioned() in C++ library
Last Updated :
04 Sep, 2021
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
#include <bits/stdc++.h>
#include <boost/algorithm/cxx11/is_partitioned.hpp>
using namespace std;
bool isOdd( int i)
{
return i % 2 == 1;
}
int main()
{
int c[] = { 1, 2, 5, 6, 8 };
bool ans
= boost::algorithm::is_partitioned(c, isOdd);
if (ans == 1)
cout << "Sequence is partitioned" ;
else
cout << "Sequence is not partitioned" ;
return 0;
}
|
Output:
Sequence is not partitioned
Program-2:
CPP
#include <bits/stdc++.h>
#include <boost/algorithm/cxx11/is_partitioned.hpp>
using namespace std;
bool isEven( int i)
{
return i % 2 == 0;
}
int main()
{
int c[] = { 4, 2, 5, 6, 8 };
bool ans
= boost::algorithm::is_partitioned(c,
c + 2,
isEven);
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
Please Login to comment...