partition_copy in C++ STL
Last Updated :
27 Nov, 2022
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;
}
OutputThe 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;
}
OutputThe elements that return true for given condition 1 2 4
The elements that return false for given condition 3 5 6
Similar Reads
std::partition in C++ STL C++ has a class in its STL algorithms library which allows us easy partition algorithms using certain inbuilt functions. Partition refers to act of dividing elements of containers depending upon a given condition. Partition operations :1. partition(beg, end, condition) :- This function is used to pa
5 min read
std::partial_sort_copy in C++ std::partial_sort is used for sorting the range within the entire container. So, if we want to keep the original container intact and just copy the sorted sub-part of the container into another one, then for that purpose, we can use std::partial_sort_copy. Just like std::partial_sort, partial_sort_c
4 min read
partition_point in C++ 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 ar
4 min read
std::is_partitioned in C++ 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 ran
3 min read
copy_n() Function in C++ STL Copy_n() is the C++ function defined in <algorithm> library in STL. It helps to copy one array element to the new array. Copy_n function allows the freedom to choose how many elements must be copied in the destination container. This function takes 3 arguments, the source array name, the size
2 min read
std :: reverse_copy in C++ STL C++ STL provides a function that copies the elements from the given range but in reverse order. Below is a simple program to show the working of reverse_copy(). Examples: Input : 1 2 3 4 5 6 7 8 9 10 Output : The vector is: 10 9 8 7 6 5 4 3 2 1 The function takes three parameters. The first two are
2 min read