Mastering std::partition: A Guide to Common Pitfalls and Alternatives

2025-09-17

std::partition is a powerful algorithm in C++ that rearranges elements in a range based on a given condition, or predicate. It moves all elements that satisfy the predicate to the beginning of the range and all elements that don't to the end. It returns an iterator to the first element that doesn't satisfy the predicate.

A lot of the time, issues with std::partition come from not understanding what it does and doesn't do.

A frequent misconception is that std::partition preserves the relative order of elements. It does not. The algorithm is only required to move the "true" elements to the front and the "false" elements to the back. The final relative order within each group is not guaranteed.

Let's say you have a vector [1, 5, 2, 8, 3, 6] and you want to partition it so that even numbers are at the beginning. A predicate like is_even(x) would be used. After std::partition, the result might be [2, 8, 6, 1, 5, 3]. Notice that 2, 8, 6 are all even, and 1, 5, 3 are all odd, but the original relative order (2 was before 8 and 6) is not preserved within the even group.

If you need to preserve the relative order, you should use std::stable_partition instead. It does the exact same thing but with the added guarantee that the relative order of elements within both the "true" and "false" groups is maintained.

Here’s a quick comparison

std::partition (Order not preserved)

#include <iostream>
#include <vector>
#include <algorithm>

bool is_even(int n) {
    return n % 2 == 0;
}

int main() {
    std::vector<int> numbers = {1, 5, 2, 8, 3, 6};
    
    // The relative order of elements is not guaranteed
    std::partition(numbers.begin(), numbers.end(), is_even);
    
    // This could print something like: 2 8 6 1 5 3
    for (int n : numbers) {
        std::cout << n << " ";
    }
    std::cout << std::endl;
    return 0;
}

std::stable_partition (Order preserved)

#include <iostream>
#include <vector>
#include <algorithm>

bool is_even(int n) {
    return n % 2 == 0;
}

int main() {
    std::vector<int> numbers = {1, 5, 2, 8, 3, 6};
    
    // The relative order of elements is preserved
    std::stable_partition(numbers.begin(), numbers.end(), is_even);
    
    // This will print: 2 8 6 1 5 3
    for (int n : numbers) {
        std::cout << n << " ";
    }
    std::cout << std::endl;
    return 0;
}

Tip
Use std::stable_partition whenever order matters. It's often a safer default choice unless you're working with extremely large data sets where the extra performance cost of std::stable_partition is a concern.

Another common mistake is ignoring the return value of std::partition. The algorithm returns an iterator pointing to the first element that did not satisfy the predicate. This iterator is super useful because it effectively divides the range into two sub-ranges
the "true" group and the "false" group.

Let's say you have a list of numbers and you want to partition them so that all even numbers are at the beginning. You can then use the returned iterator to, for example, process only the odd numbers.

Example of what NOT to do

// This is not very useful because you don't know where the 'true' part ends
std::vector<int> numbers = {1, 2, 3, 4, 5, 6};
std::partition(numbers.begin(), numbers.end(), is_even);
// Now what? You have to search again to find the start of the 'false' part

Example of a better way (using the return value)

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

bool is_even(int n) {
    return n % 2 == 0;
}

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5, 6};
    
    auto partition_point = std::partition(numbers.begin(), numbers.end(), is_even);
    
    // Now you have two distinct ranges:
    // [numbers.begin(), partition_point) for even numbers
    // [partition_point, numbers.end()) for odd numbers
    
    std::cout << "Even numbers: ";
    for (auto it = numbers.begin(); it != partition_point; ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    std::cout << "Odd numbers: ";
    for (auto it = partition_point; it != numbers.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

Using the return value is crucial for correctly working with the partitioned data. It allows you to treat the single range as two separate, meaningful sub-ranges without any extra work.


cpp



Demystifying C++ Empty Base Optimization

Empty base optimization (EBO) is a C++ compiler optimization that allows an empty base class to not take up any space within a derived class object


Common Pitfalls of std::basic_regex and How to Avoid Them

First, it's important to understand what std::basic_regex::~basic_regex does. It's the destructor for the regular expression object


A Developer's Guide to std::make_reverse_iterator in C++

A frequent mistake is trying to use std::make_reverse_iterator on an iterator that doesn't have the necessary capabilities



A Guide to std::geometric_distribution::p for C++ Developers

The p parameter in std::geometric_distribution is a crucial part of defining the probability of a successful trial. Understanding this parameter is key to using the distribution correctly


C++ Ranges: A Better Way to Handle Adjacent Elements

ranges::adjacent_view is designed to create a view of adjacent elements in a sequence. For example, if you have {1, 2, 3, 4}, an adjacent_view with a window of two would give you { (1, 2), (2, 3), (3, 4) }


Troubleshooting C++ Templates: A Friendly Guide

C++ templates are a powerful feature that allows you to write generic code that can work with different data types without having to rewrite the same function or class for each type


Common Pitfalls of std::mersenne_twister_engine::discard and How to Fix Them

The std::mersenne_twister_engine is a pseudorandom number generator (PRNG). Think of it like a deck of cards that's been shuffled in a very specific


std::lognormal_distribution in C++: A Friendly Guide to Parameters (m, s) and the Normal Connection

Let's dive into the common issues, parameters, and alternative approaches in a friendly way!First, let's quickly review what std::lognormal_distribution does


C++ std::seed_seq: Common Problems & Alternatives

While std::seed_seq is powerful, there are a few common issues people run into.The biggest misunderstanding is that std::seed_seq itself is a random number generator


Troubleshooting std::isalnum in C++ Strings

The std::isalnum function is super useful for checking if a character is an alphabet (A-Z, a-z) or a digit (0-9). However