Namespaces
Variants
Views
Actions

std::ranges::partition

From cppreference.com
< cpp‎ | algorithm‎ | ranges
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms: std::ranges::copy, std::ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
Operations on uninitialized storage
Partitioning operations
Sorting operations
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
Minimum/maximum operations
(C++11)
(C++17)

Permutations
Numeric operations
C library
 
Constrained algorithms
Non-modifying sequence operations
Modifying sequence operations
Operations on uninitialized storage
Partitioning operations
ranges::partition
Sorting operations
Binary search operations
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutations
 
Defined in header <algorithm>
Call signature
template< std::permutable I, std::sentinel_for<I> S, class Proj = std::identity,

          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
  constexpr ranges::subrange<I>

partition( I first, S last, Pred pred, Proj proj = {} );
(1) (since C++20)
template<ranges::forward_range R, class Proj = std::identity,

         std::indirect_unary_predicate<
             std::projected<ranges::iterator_t<R>, Proj>> Pred>
requires std::permutable<ranges::iterator_t<R>>
  constexpr ranges::borrowed_subrange_t<R>

partition(R&& r, Pred pred, Proj proj = {});
(2) (since C++20)
1) Reorders the elements in the range [first, last) in such a way that the projection proj of all elements for which the predicate pred returns true precede the projection proj of elements for which predicate pred returns false. Relative order of elements is not preserved.
2) Same as (1), but uses r as the source range, as if using ranges::begin(r) as first and ranges::end(r) as last.

The function-like entities described on this page are niebloids, that is:

In practice, they may be implemented as function objects, or with special compiler extensions.

Contents

[edit] Parameters

first, last - the range of elements to reorder
r - the range of elements to reorder
pred - predicate to apply to the projected elements
proj - projection to apply to the elements

[edit] Return value

A subrange starting with an iterator to the first element of the second group and finishing with an iterator equal to last. (2) returns std::ranges::dangling if r is an rvalue of non-borrowed_range type.

[edit] Complexity

Given N = ranges::distance(first,last).

Exactly N applications of the predicate and projection. At most N/2 swaps if I models ranges::bidirectional_iterator, and at most N swaps otherwise.

[edit] Possible implementation

struct partition_fn {
  template<std::permutable I, std::sentinel_for<I> S, class Proj = std::identity,
           std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
  constexpr ranges::subrange<I>
  operator()(I first, S last, Pred pred, Proj proj = {}) const
  {
      first = ranges::find_if_not(first, last, std::ref(pred), std::ref(proj));
      if (first == last) {
          return {first, first};
      }
 
      auto begin = first;
      for (auto i = ranges::next(first); i != last; ++i) {
          if (std::invoke(pred, std::invoke(proj, *i))) {
              ranges::iter_swap(i, first);
              ++first;
          }
      }
      return {std::move(begin), std::move(first)};
  }
 
  template<ranges::forward_range R, class Proj = std::identity,
           std::indirect_unary_predicate<
               std::projected<ranges::iterator_t<R>, Proj>> Pred>
  requires std::permutable<ranges::iterator_t<R>>
  constexpr ranges::borrowed_subrange_t<R>
  operator()(R&& r, Pred pred, Proj proj = {}) const
  {
    return (*this)(ranges::begin(r), ranges::end(r),
                   std::ref(pred), std::ref(proj));
  }
};
 
inline constexpr partition_fn partition;

[edit] Example

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <forward_list>
 
namespace ranges = std::ranges;
 
template <std::permutable I, std::sentinel_for<I> S>
void quicksort(I first, S last)
{
   if(first == last) {
       return;
   }
 
   auto pivot = *ranges::next(first, ranges::distance(first,last)/2, last);
   std::ranges::subrange tail1 = ranges::partition(first, last, [pivot](const auto& em){
       return em < pivot;
   });
   std::ranges::subrange tail2 = ranges::partition(tail1, [pivot](const auto& em){
       return !(pivot < em);
   });
   quicksort(first, tail1.begin());
   quicksort(tail2.begin(), tail2.end());
}
 
int main()
{
    std::vector<int> v = {0,1,2,3,4,5,6,7,8,9};
    std::cout << "Original vector:\n    ";
    for(int elem : v) {
        std::cout << elem << ' ';
    }
 
    auto tail = ranges::partition(v, [](int i){return i % 2 == 0;});
 
    std::cout << "\nPartitioned vector:\n    ";
    ranges::copy(ranges::begin(v), ranges::begin(tail), std::ostream_iterator<int>(std::cout, " "));
    std::cout << " * ";
    ranges::copy(tail, std::ostream_iterator<int>(std::cout, " "));
 
    std::forward_list<int> fl = {1, 30, -4, 3, 5, -4, 1, 6, -8, 2, -5, 64, 1, 92};
    std::cout << "\nUnsorted list:\n    ";
    for(int n : fl) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
 
    quicksort(ranges::begin(fl), ranges::end(fl));
    std::cout << "Sorted using quicksort:\n    ";
    for(int fi : fl) {
        std::cout << fi << ' ';
    }
    std::cout << '\n';
}

Output:

Original vector:
    0 1 2 3 4 5 6 7 8 9 
Partitioned vector:
    0  * 2 4 6 8 
Unsorted list:
    1 30 -4 3 5 -4 1 6 -8 2 -5 64 1 92 
Sorted using quicksort:
    -4 -4 -8 -5 1 1 3 2 5 1 30 64 6 92

[edit] See also

determines if the range is partitioned by the given predicate
(niebloid) [edit]
divides elements into two groups while preserving their relative order
(niebloid) [edit]
divides a range of elements into two groups
(function template) [edit]