C++ std::partition() Function

Definition
In C++, std::partition() rearranges a range into two groups. All elements for which the predicate is true come first, and the rest follow.
It returns an iterator to the start of the second group. The order inside each group is not kept. Use std::stable_partition() if you need that.
Syntax
Add #include <algorithm>, then call std::partition(). Start with these two lines.
auto mid = std::partition(v.begin(), v.end(), pred);
std::partition(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
| Call | What it does |
|---|
auto mid = std::partition(v.begin(), v.end(), pred); | Split into two groups and get the border. |
std::partition(v.begin(), v.end(), [](int n) { return n % 2 == 0; }); | Move the even numbers to the front. |
Library declarations
template<class ForwardIt, class UnaryPred>
ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPred p);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to split. |
| p | Required. A predicate. Elements for which it returns true go to the front. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points at the first element of the second group (the elements that failed the predicate). |
C++ Examples
Example 1 — Evens first
Split a vector into even and odd numbers.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto mid = std::partition(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
std::sort(v.begin(), mid);
std::sort(mid, v.end());
std::cout << "Evens: ";
for (auto it = v.begin(); it != mid; ++it) std::cout << *it << ' ';
std::cout << '\n';
std::cout << "Odds: ";
for (auto it = mid; it != v.end(); ++it) std::cout << *it << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The predicate is true for even numbers, so they are moved to the front.
mid marks the border between the two groups.- We sort each group only to get the same print on every compiler.
Example 2 — Negatives first
Count how many numbers moved to the front.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, -1, 4, -5, 0};
auto mid = std::partition(v.begin(), v.end(), [](int n) { return n < 0; });
std::cout << "Negative count: " << (mid - v.begin()) << '\n';
return 0;
}
How It Works
- Only -1 and -5 are below zero.
- They are moved to the front, so the border
mid is two places from the start. mid - v.begin() is therefore the number of negative values.
Example 3 — Pass or fail
Group scores by a pass mark.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores = {45, 80, 60, 30, 95, 59};
auto mid = std::partition(scores.begin(), scores.end(), [](int s) { return s >= 60; });
bool frontOk = std::all_of(scores.begin(), mid, [](int s) { return s >= 60; });
std::cout << "Passed: " << (mid - scores.begin()) << " of " << scores.size() << '\n';
std::cout << std::boolalpha << "All in front pass: " << frontOk << '\n';
return 0;
}
Passed: 3 of 6
All in front pass: true
How It Works
- The scores 80, 60 and 95 meet the pass mark of 60.
- They are moved before
mid, so three scores are in the first group. all_of() confirms that the whole front group follows the rule.
Frequently Asked Questions
In C++, std::partition() reorders a range so elements that satisfy a condition come first. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the first element of the second group, where the predicate is false.
partition() only splits into two groups, which is faster. sort() puts every element in order.
No. Use std::stable_partition() when the order must be kept.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
7 people found this page helpful