boost::range::partition

References

Headers

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

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

Examples

partition.cpp

#include <functional>
#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};

    // partition() reorders the input range so that all elements for which
    // the predicate function is true come first. All others come second.
    // Returns an iterator to the first element of the second group.
    auto middle = boost::range::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 1 0 2 4 8 6 5 7 9 
--------- ---------
   < 5      >= 5

 

Boost Range for Humans

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