Determining Partition Size

How can I determine the size of each partition after using the partition() function?

After using the partition() function, you can determine the size of each partition by examining the subrange returned by the function.

This subrange represents the second partition, where the predicate returned false. The elements before this subrange belong to the first partition.

Here's an example to illustrate how you can find the size of each partition:

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

int main() {
  std::vector<int> A = {1, -6, 7, 2, 5, 4};

  auto isEven = [](int x) { return x % 2 == 0; };

  auto SecondSR = std::ranges::partition(A, isEven);

  // Size of the second partition
  std::cout << "Second Partition Size: "
            << SecondSR.size() << '\n';

  // Create a subrange for the first partition
  auto FirstSR = std::ranges::subrange(
    A.begin(), SecondSR.begin());

  // Size of the first partition
  std::cout << "First Partition Size: "
            << FirstSR.size() << '\n';

  // Output elements in both partitions
  std::cout << "First Partition: ";
  for (int x : FirstSR) std::cout << x << ", ";
  std::cout << "\nSecond Partition: ";
  for (int x : SecondSR) std::cout << x << ", ";
}
Second Partition Size: 3
First Partition Size: 3
First Partition: 4, -6, 2,
Second Partition: 7, 5, 1,

In this example:

  • The isEven predicate determines if an element is even.
  • std::ranges::partition() partitions the vector A into even and odd numbers.
  • SecondSR represents the subrange of odd numbers.
  • FirstSR is constructed using iterators from the beginning of A to the beginning of SecondSR.

By calculating the sizes of FirstSR and SecondSR, you get the sizes of the first and second partitions, respectively. This method allows you to work with partitions efficiently after using partition().

Partition Algorithms

An introduction to partitions, and the C++ standard library algorithms that create them

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Partition vs Stable Partition Performance
How does the partition() algorithm differ from the stable_partition() algorithm in terms of performance?
Partitioning Custom Data Types
Can the partition() function be used with custom data types, and if so, how?
When to Use partition_copy()
What are the use cases for partition_copy() over partition() or stable_partition()?
Understanding partition_point()
How does the partition_point() algorithm determine the boundary between partitions?
Safe Iterator Handling in partition_point()
How can I handle the iterator returned by partition_point() to avoid dereferencing issues?
Partitioning Without Random Access Iterators
Is it possible to use the partition() algorithm with containers that do not support random access iterators?
Alternatives to partition()
Are there any alternatives to using std::partition() or std::ranges::partition() that offer better performance or additional features?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant