When to Use partition_copy()

What are the use cases for partition_copy() over partition() or stable_partition()?

The partition_copy() algorithm is useful when you need to partition a collection into two separate containers without modifying the original container.

This is in contrast to partition() and stable_partition(), which rearrange elements in place. Here are some common use cases for partition_copy():

Preserving Original Data

If you need to maintain the original collection for further use or comparisons, partition_copy() allows you to create new partitions without altering the original data.

Separating Data into New Containers

When you need the partitions to exist in separate containers, partition_copy() provides a clean and efficient way to achieve this. For example, you might want to separate even and odd numbers into two different vectors:

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

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

  Even.resize(A.size());
  Odd.resize(A.size());

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

  auto result = std::ranges::partition_copy(
    A, Even.begin(), Odd.begin(), isEven);

  // Adjusting sizes of Even and Odd vectors
  // based on the result
  Even.resize(result.out1 - Even.begin());
  Odd.resize(result.out2 - Odd.begin());

  std::cout << "Original: ";
  for (int x : A) std::cout << x << ", ";
  std::cout << "\nEven: ";
  for (int x : Even) std::cout << x << ", ";
  std::cout << "\nOdd: ";
  for (int x : Odd) std::cout << x << ", ";
}
Original: 1, -6, 4, 2, 5, 7,
Even: -6, 4, 2,
Odd: 1, 5, 7,

Memory Considerations

If your algorithm requires the original data and the partitions to be processed in parallel or by different functions, keeping them in separate containers can simplify memory management and reduce the risk of accidental modifications.

Algorithm Constraints

Some algorithms or processing steps might require the data to be in separate containers for performance or design reasons. Using partition_copy() can help in such scenarios by directly producing the required data structure.

Conclusion

Use partition_copy() when you need to keep the original collection unchanged and want the partitions to exist in separate containers. It's particularly useful for preserving original data, managing memory more effectively, and complying with specific algorithmic constraints.

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?
Determining Partition Size
How can I determine the size of each partition after using the partition() function?
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