C++ std::stable_partition() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

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.

C++
auto mid = std::stable_partition(v.begin(), v.end(), pred);
std::stable_partition(v.begin(), v.end(), [](int n) { return n != 0; });
CallWhat 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
C++
template<class BidirIt, class UnaryPred>
BidirIt stable_partition(BidirIt first, BidirIt last, UnaryPred p);

Parameters

ParameterDescription
first, lastRequired. The range to split.
pRequired. A predicate. Elements for which it returns true go to the front.

Return Value

Return ValueDescription
IteratorPoints at the first element of the second group.

C++ Examples

Example 1 — Evens first, order kept

Move even numbers to the front.

C++
#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

  1. The even numbers 2, 4 and 6 go first, in their original order.
  2. The odd numbers 1, 3 and 5 follow, also in their original order.
  3. No sorting was needed to get this output.

Example 2 — Short words first

Group strings by length.

C++
#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;
}

How It Works

  1. Words with up to four letters are fig and kiwi.
  2. They move to the front and keep their order, with fig before kiwi.
  3. banana, cherry and apple keep their order too.

Example 3 — Zeros to the end

Keep the order of non-zero values.

C++
#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

  1. The predicate is true for numbers that are not zero.
  2. 3, 5 and 7 stay in the same order at the front.
  3. 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.

Next: stable_sort()

Sorts a range and keeps the order of equal elements.

stable_sort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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