Open In App
Related Articles

std::is_partitioned in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
std::is_partitioned is used for finding whether the range[first, last) is partitioned or not. A range is said to be partitioned with respect to a condition if all the elements for which the condition evaluates to true precede those for which it is false. It is defined in the header file . If the range in which we want to check whether it is partitioned or not is empty, then this function returns true. Syntax:
 bool is_partitioned (InputIterator first, 
                      InputIterator last, UnaryPredicate pred);

first: Input iterator to the first element in the range.
last: Input iterator to the last element in the range.
pred: Unary function that accepts an element in the 
range as argument, and returns a value convertible to bool. 
The value returned indicates whether the element belongs to
the first group (if true, the element is expected before all
the elements for which it returns false).
The function shall not modify its argument.
This can either be a function pointer or a function object.


Returns: It returns true if all the elements in the range [first, last)
for which pred returns true precede those for which it returns false.
Otherwise it returns false.
If the range is empty, the function returns true.

// C++ program to demonstrate the use of std::is_partitioned
#include <iostream>
#include <algorithm>
#include <vector>
  
// Defining the BinaryFunction
bool pred(int a)
{
    return (a % 3 == 0);
}
  
using namespace std;
int main()
{
    // Declaring first vector
    vector<int> v1 = { 3, 6, 9, 10, 11, 13 };
  
    // Using std::is_partitioned
    bool b = std::is_partitioned(v1.begin(), v1.end(), pred);
  
    if (b == 1) {
        cout << "It is partitioned";
    } else {
        cout << "It is not partitioned.";
    }
    return 0;
}

                    
Output:
It is partitioned
Explanation: Here, in this program firstly, we have stored elements in a vector, and then we are checking whether all the elements divisible by 3 are present before those which are not divisible by 3. Since, this condition evaluates to true for the taken vector therefore, this function returns 1 here, as it is partitioned.

Another Example

  • To check whether all the odd and even elements are partitioned
    // C++ program to demonstrate the use of std::is_partitioned
    #include <iostream>
    #include <algorithm>
    #include <vector>
      
    // Defining the BinaryFunction
    bool pred(int a)
    {
        return (a % 2 == 0);
    }
      
    using namespace std;
    int main()
    {
        // Declaring first vector
        vector<int> v1 = { 2, 4, 6, 3, 5, 7, 9 };
      
        // Using std::is_partitioned
        bool b = std::is_partitioned(v1.begin(), v1.end(), pred);
      
        if (b == 1) {
            cout << "All the even no. are present before odd no.";
        } else {
            cout << "All the even no. are not present before odd no.";
        }
      
        // Inserting an even no. at the end of v1
        // so std::is_partitioned returns false
        v1.push_back(16);
      
        // Now again using std::is_partitioned
        b = std::is_partitioned(v1.begin(), v1.end(), pred);
      
        if (b == 1) {
            cout << "\nAll the even no. are present before odd no.";
        } else {
            cout << "\nAll the even no. are not present before odd no.";
        }
      
        return 0;
    }
    
                        
    Output:
    All the even no. are present before odd no.
    All the even no. are not present before odd no.
    


Last Updated : 02 Aug, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads