Skip to content
Related Articles
Open in App
Not now

Related Articles

partition_point in C++

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 05 Aug, 2018
Improve Article
Save Article
Like Article

partition_point() Gets the partition point : Returns an iterator to the first element in the partitioned range [first, last) for which pred(predicate) is not true, indicating its partition point.

The elements in the range shall already be partitioned, as if partition had been called with the same arguments.

The function optimizes the number of comparisons performed by comparing non-consecutive elements of the sorted range, which is specially efficient for random-access iterators.

Syntax :

  ForwardIterator partition_point(ForwardIterator first, 
                                  ForwardIterator last,
                                  UnaryPredicate pred); 

Parameters :
first, last :
Forward iterators to the initial and final positions of the partitioned sequence. The range checked is [first, last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

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 goes before the partition point (if true, it goes before; if false goes at or after it). The function shall not modify its argument. This can either be a function pointer or a function object.

Return Value :
An iterator to the first element in the partitioned range [first, last) for which pred is not true, or last if it is not true for any element.

The definition of this function template is equivalent to:

template 
  ForwardIterator partition_point (ForwardIterator first, ForwardIterator last,
                                   UnaryPredicate pred)
{
  auto n = distance(first, last);
  while (n>0)
  {
    ForwardIterator it = first;
    auto step = n/2;
    std::advance (it, step);
    if (pred(*it)) { first=++it; n-=step+1; }
    else n=step;
  }
  return first;
}

Example 1:




// C++ program to get odd elements
// and even elements
#include <iostream> // std::cout
#include <algorithm> // std::partition, std::partition_point
#include <vector> // std::vector
  
bool IsOdd(int i) { return (i % 2) == 1; }
  
int main()
{
    std::vector<int> data{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    std::vector<int> odd, even;
  
    // partition data on the basis of odd elements 
    // using pred IsOdd()
    std::stable_partition(data.begin(), data.end(), IsOdd);
  
    // gets the partition point based on odd elements
    auto it = std::partition_point(data.begin(), data.end(), 
                                                     IsOdd);
  
    // assign elements to odd from beginning till partition 
    // point.
    odd.assign(data.begin(), it);
    even.assign(it, data.end());
  
    // print contents of odd:
    std::cout << "odd:";
    for (int& x : odd)
        std::cout << ' ' << x;
    std::cout << '\n';
  
    // print contents of even:
    std::cout << "even:";
    for (int& x : even)
        std::cout << ' ' << x;
    std::cout << '\n';
  
    return 0;
}

Output:

odd:  1 3 5 7 9
even: 2 4 6 8 10

Example 2:




// C++ program to get negative elements
// and positive elements using partition_point
#include <iostream> // std::cout
#include <algorithm> // std::partition, std::partition_point
#include <vector> // std::vector
  
bool IsNegative(int i) { return (i < 0); }
  
int main()
{
    std::vector<int> data{ 1, -1, 3, -4, 5, 2, -2, 4, 
                                             -5, -3 };
    std::vector<int> negative, positive;
  
    // partition data on the basis of odd elements using 
    // pred IsNegative()
    std::stable_partition(data.begin(), data.end(), 
                                       IsNegative);
  
    // gets the partition point based on odd elements
    auto it = std::partition_point(data.begin(), data.end(), 
                                                 IsNegative);
  
    // assign elements to odd from beginning till
    // partition point.
    negative.assign(data.begin(), it);
    positive.assign(it, data.end());
  
    // print contents of odd:
    std::cout << "Negative: ";
    for (int& x : negative)
        std::cout << ' ' << x;
    std::cout << '\n';
  
    // print contents of even:
    std::cout << "Positive: ";
    for (int& x : positive)
        std::cout << ' ' << x;
    std::cout << '\n';
  
    return 0;
}

Output:

Negative:  -1 -4 -2 -5 -3
Positive:  1 3 5 2 4

Complexity :
Performs approximately log2(N)+2 element comparisons (where N is this distance). On non-random-access iterators, the iterator advances produce themselves an additional linear complexity in N on average.

This article is contributed by Shubham Rana. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!