std::partition in C++ STL
Last Updated :
10 Dec, 2021
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 partition the elements on basis of condition mentioned in its arguments.
2. is_partitioned(beg, end, condition) :- This function returns boolean true if container is partitioned else returns false.
CPP
// C++ code to demonstrate the working of
// partition() and is_partitioned()
#include<iostream>
#include<algorithm> // for partition algorithm
#include<vector> // for vector
using namespace std;
int main()
{
// Initializing vector
vector<int> vect = { 2, 1, 5, 6, 8, 7 };
// Checking if vector is partitioned
// using is_partitioned()
is_partitioned(vect.begin(), vect.end(), [](int x)
{
return x%2==0;
})?
cout << "Vector is partitioned":
cout << "Vector is not partitioned";
cout << endl;
// partitioning vector using partition()
partition(vect.begin(), vect.end(), [](int x)
{
return x%2==0;
});
// Checking if vector is partitioned
// using is_partitioned()
is_partitioned(vect.begin(), vect.end(), [](int x)
{
return x%2==0;
})?
cout << "Now, vector is partitioned after partition operation":
cout << "Vector is still not partitioned after partition operation";
cout << endl;
// Displaying partitioned Vector
cout << "The partitioned vector is : ";
for (int &x : vect) cout << x << " ";
return 0;
}
Output:
Vector is not partitioned
Now, vector is partitioned after partition operation
The partitioned vector is : 2 8 6 5 1 7
In the above code, partition function partitions the vector depending on whether an element is even or odd, even elements are partitioned from odd elements in no particular order.
3. stable_partition(beg, end, condition) :- This function is used to partition the elements on basis of condition mentioned in its arguments in such a way that the relative order of the elements is preserved..
4. partition_point(beg, end, condition) :- This function returns an iterator pointing to the partition point of container i.e. the first element in the partitioned range [beg,end) for which condition is not true. The container should already be partitioned for this function to work.
CPP
// C++ code to demonstrate the working of
// stable_partition() and partition_point()
#include<iostream>
#include<algorithm> // for partition algorithm
#include<vector> // for vector
using namespace std;
int main()
{
// Initializing vector
vector<int> vect = { 2, 1, 5, 6, 8, 7 };
// partitioning vector using stable_partition()
// in sorted order
stable_partition(vect.begin(), vect.end(), [](int x)
{
return x%2 == 0;
});
// Displaying partitioned Vector
cout << "The partitioned vector is : ";
for (int &x : vect) cout << x << " ";
cout << endl;
// Declaring iterator
vector<int>::iterator it1;
// using partition_point() to get ending position of partition
auto it = partition_point(vect.begin(), vect.end(), [](int x)
{
return x%2==0;
});
// Displaying partitioned Vector
cout << "The vector elements returning true for condition are : ";
for ( it1= vect.begin(); it1!=it; it1++)
cout << *it1 << " ";
cout << endl;
return 0;
}
Output:
The partitioned vector is : 2 6 8 1 5 7
The vector elements returning true for condition are : 2 6 8
In the above code, even and odd elements are partitioned and in the increasing order (sorted). Not always in increasing order though, here the elements (even and odd) appeared in increased order so is the result after partition. if vect would have been { 2,1,7,8,6,5 } after stable_partition() it would be { 2,8,6,1,7,5 }. The order of appearance is maintained.
5. partition_copy(beg, end, beg1, beg2, condition) :- This function copies the partitioned elements in the different containers mentioned in its arguments. It takes 5 arguments. Beginning and ending position of container, beginning position of new container where elements have to be copied (elements returning true for condition), beginning position of new container where other elements have to be copied (elements returning false for condition) and the condition. Resizing new containers is necessary for this function.
CPP
// C++ code to demonstrate the working of
// partition_copy()
#include<iostream>
#include<algorithm> // for partition algorithm
#include<vector> // for vector
using namespace std;
int main()
{
// Initializing vector
vector<int> vect = { 2, 1, 5, 6, 8, 7 };
// Declaring vector1
vector<int> vect1;
// Declaring vector1
vector<int> vect2;
// Resizing vectors to suitable size using count_if() and resize()
int n = count_if (vect.begin(), vect.end(), [](int x)
{
return x%2==0;
} );
vect1.resize(n);
vect2.resize(vect.size()-n);
// Using partition_copy() to copy partitions
partition_copy(vect.begin(), vect.end(), vect1.begin(),
vect2.begin(), [](int x)
{
return x%2==0;
});
// Displaying partitioned Vector
cout << "The elements that return true for condition are : ";
for (int &x : vect1)
cout << x << " ";
cout << endl;
// Displaying partitioned Vector
cout << "The elements that return false for condition are : ";
for (int &x : vect2)
cout << x << " ";
cout << endl;
return 0;
}
Output:
The elements that return true for condition are : 2 6 8
The elements that return false for condition are : 1 5 7
Similar Reads
C++ STL Algorithm Library
Standard Template Library (STL) offers a rich collection of algorithms designed to operate on STL containers and beyond. It provides commonly used algorithms such as sorting, searching, copying, etc. These well tested algorithms are optimized for performance and provide a way to write cleaner, faste
3 min read
sort() in C++ STL
In C++, sort() is a built-in function used to sort the given range in desired order. It provides a simple and efficient way to sort the data in C++, but it only works on data structures that provide random access to its elements such as vectors and arrays.Let's take a look at an example:C++#include
4 min read
Type Inference in C++ (auto and decltype)
Type Inference is a feature in C++, using which the compiler automatically deduces the data type of an expression, function, or variable. Type inference was introduced with C++11 through the use of the auto and decltype.Before C++ 11, each data type had to be explicitly declared, which limited the v
5 min read
transform() in C++ STL
In C++, transform() is a built-in STL function used to apply the given operation to a range of elements and store the result in another range. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { vector<i
4 min read
Variadic function templates in C++
Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue. Douglas Gregor
3 min read
Template Specialization in C++
Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type. What if we want a different code for a p
5 min read
Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
Some of the merge operation classes are provided in C++ STL under the header file "algorithm", which facilitates several merge operations in a easy manner. Some of them are mentioned below. merge(beg1, end1, beg2, end2, beg3) :- This function merges two sorted containers and stores in new container
7 min read
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
accumulate() and partial_sum() in C++ STL
accumulate() and partial_sum() functions are used to find the sum or any other accumulated value that is obtained by doing the addition or any other binary operation on the elements in the given range. Both of these functions are the part of STL Numeric Library and defined inside <numeric> hea
4 min read
numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
The numeric header is part of the numeric library in C++ STL. This library consists of basic mathematical functions and types, as well as optimized numeric arrays and support for random number generation. Some of the functions in the numeric header: iotaaccumulatereduceinner_productpartial_sum etc.
4 min read