Open In App

partition_copy in C++ STL

Last Updated : 27 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

partition_copy is the inbuilt function defined in <algorithm> library in STL. partition_copy function duplicates the partitioned elements into the various containers given in its parameters. It requires 5 arguments which are the beginning and ending position of the container, the beginning position of a new container where elements have to be copied (elements returning true for condition), the beginning position of a new container where other elements have to be copied (elements returning false for condition) and the condition. For this function, new containers must be resized.

Syntax:

partition_copy(iterator it1_first, iterator it1_end, iterator it2_first, iterator it3_first, cond)

Parameters:

iterator it1_first: Iterator pointing to the start of initial container from which we want to do the partition.

iterator it1_end: Iterator pointing to the end of initial container from which we want to do the partition.

iterator it2_first: Iterator pointing to the start of new container where other elements have to be copied based on the conditions specified.

iterator it3_first: Iterator pointing to the start of another new container where other elements have to be copied based on the conditions specified.

cond: A user defined function which help to partition the element into new containers based on the condition in this function.

Time and Space Complexity:

Time Complexity: O(n) 
Auxiliary Space: O(n)

where n is the number of elements in the initial vector.

Example 1: Using the partition_copy() function to divide the given vector into two parts based on the condition that the number is even or odd in the vector.

Code:

C++




// C++ code to demonstrate the working of
// partition_copy()
#include<iostream>
#include<algorithm>
#include<vector>
  
using namespace std;
int main()
{
    // Initializing the vector
    vector<int> arr = { 1, 2, 3, 4, 5, 6 };
      
    // Declaring vectors for storing
    // partitioned elements
    vector<int> arr1,arr2;
      
    // Resizing vectors to suitable size using 
    // count_if() and resize()
    int n = count_if (arr.begin(), arr.end(), [](int x)
    {
        return x%2==0;
          
    } );
    arr1.resize(n);
    arr2.resize(arr.size()-n);
      
    // Using partition_copy() to copy partitions
    partition_copy(arr.begin(), arr.end(), arr1.begin(),
                                arr2.begin(), [](int x)
    {
        return x%2==0;
    });
      
    // Displaying partitioned vectors
    cout << "The elements that return true for given condition ";
    for (int &x : arr1)
            cout << x << " ";
    cout << endl;
      
    cout << "The elements that return false for given condition ";
    for (int &x : arr2)
            cout << x << " ";
    cout << endl;
      
    return 0;
}


Output

The elements that return true for given condition 2 4 6 
The elements that return false for given condition 1 3 5 

Example 2: Using the partition_copy() function to divide the given vector into two parts based on the condition that the number is a power of two or not.

Code:

C++




// C++ code to demonstrate the working of
// partition_copy()
#include<bits/stdc++.h>
  
using namespace std;
  
int main()
{
    // Initializing the vector
    vector<int> arr = { 1, 2, 3, 4, 5, 6 };
      
    // Declaring vectors for storing
    // partitioned elements
    vector<int> arr1,arr2;
      
    // Resizing vectors to suitable size using 
    // count_if() and resize()
    int n = count_if (arr.begin(), arr.end(), [](int x)
    {
        if (x == 0)
        return false;
   
        return (ceil(log2(x)) == floor(log2(x)));
          
    } );
    arr1.resize(n);
    arr2.resize(arr.size()-n);
      
    // Using partition_copy() to copy partitions
    partition_copy(arr.begin(), arr.end(), arr1.begin(),
                                arr2.begin(), [](int x)
    {
      // code to check if number is power of two or not
        if (x == 0)
        return false;
   
        return (ceil(log2(x)) == floor(log2(x)));
    });
      
    // Displaying partitioned vectors
    cout << "The elements that return true for given condition ";
    for (int &x : arr1)
            cout << x << " ";
    cout << endl;
      
    cout << "The elements that return false for given condition ";
    for (int &x : arr2)
            cout << x << " ";
    cout << endl;
      
    return 0;
}


Output

The elements that return true for given condition 1 2 4 
The elements that return false for given condition 3 5 6 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads