partition_point in C++


In this tutorial, we will be discussing a program to understand partition_point in C++.

Partition point is a method which returns an iterator pointing to the first value in a given range. The range is a partitioned one in which the predicate is not true.

Example

 Live Demo

#include <iostream>
#include <algorithm>
#include <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;
   std::stable_partition(data.begin(), data.end(), IsOdd);
   auto it = std::partition_point(data.begin(), data.end(),
   IsOdd);
   odd.assign(data.begin(), it);
   even.assign(it, data.end());
   std::cout << "odd:";
   for (int& x : odd)
      std::cout << ' ' << x;
   std::cout << '\n';
   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

Updated on: 14-Apr-2020

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements