boost::range::stable_partition

References

Headers

boost::range::stable_partition is available by including any of the following headers:

  • boost/range/algorithm/stable_partition.hpp or
  • boost/range/algorithm.hpp

Examples

stable_partition.cpp

#include <iostream>
#include <vector>
#include <boost/range/algorithm.hpp>

void display_result(const std::vector<int> vec) {
    boost::range::copy(vec, std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "--------- ---------" << std::endl;
    std::cout << "   < 5      >= 5" << std::endl;
}

int main() {
    std::vector<int> vec = {3, 9, 0, 6, 4, 8, 2, 5, 7, 1};

    // stable_partition() reorders the input range so that all elements for
    // which the predicate function is true come first. All others come second.
    // Compared to partition(), this function keeps the relative order of
    // elements within each group.
    // Returns an iterator to the first element of the second group.
    auto middle = boost::range::stable_partition(vec, [](int v) { return v < 5; });

    std::cout << (middle - vec.begin()) << " elements are < 5" << std::endl << std::endl;
    display_result(vec);

    return 0;
}

Output:

5 elements are < 5

3 0 4 2 1 9 6 8 5 7 
--------- ---------
   < 5      >= 5

 

Boost Range for Humans

This reference is part of Boost Range for Humans. Click the link to the overview.