C++ std::stable_partition() Function

Definition
In C++, std::stable_partition() splits a range like std::partition(), but elements keep their original relative order within each group.
This makes the result predictable, so you can print it without sorting. It may use extra memory to do this.
Syntax
Add #include <algorithm>, then call std::stable_partition(). Start with these two lines.
auto mid = std::stable_partition(v.begin(), v.end(), pred);
std::stable_partition(v.begin(), v.end(), [](int n) { return n != 0; });
| Call | What it does |
|---|
auto mid = std::stable_partition(v.begin(), v.end(), pred); | Split and keep the order. Get the border. |
std::stable_partition(v.begin(), v.end(), [](int n) { return n != 0; }); | Push zeros to the back. |
Library declarations
template<class BidirIt, class UnaryPred>
BidirIt stable_partition(BidirIt first, BidirIt 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. |
C++ Examples
Example 1 — Evens first, order kept
Move even numbers to the front.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 6};
std::stable_partition(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The even numbers 2, 4 and 6 go first, in their original order.
- The odd numbers 1, 3 and 5 follow, also in their original order.
- No sorting was needed to get this output.
Example 2 — Short words first
Group strings by length.
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> words = {"banana", "fig", "cherry", "kiwi", "apple"};
std::stable_partition(words.begin(), words.end(),
[](const std::string& w) { return w.size() <= 4; });
for (const std::string& x : words) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
fig kiwi banana cherry apple
How It Works
- Words with up to four letters are
fig and kiwi. - They move to the front and keep their order, with
fig before kiwi. banana, cherry and apple keep their order too.
Example 3 — Zeros to the end
Keep the order of non-zero values.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {0, 3, 0, 5, 7, 0};
std::stable_partition(v.begin(), v.end(), [](int n) { return n != 0; });
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The predicate is true for numbers that are not zero.
- 3, 5 and 7 stay in the same order at the front.
- The three zeros are collected at the back.
Frequently Asked Questions
In C++, std::stable_partition() reorders a range into two groups and keeps the order inside each group. 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.
Use it when the order of the elements inside each group matters, for example in a list shown to the user.
It can be, because it must keep the order. It may also use extra memory.
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.
12 people found this page helpful