Partitioning Custom Data Types

Can the partition() function be used with custom data types, and if so, how?

Yes, the partition() function can be used with custom data types in C++. To do this, you need to define a predicate that operates on the members of your custom type.

The predicate determines how the objects are divided into partitions. Consider the following custom type Actor:

#include <string>

enum class Mood { Friendly, Neutral, Hostile };

class Actor {
public:
  Actor(std::string Name, Mood Mood)
    : Name{Name}, Mood{Mood} {}

  std::string Name;
  Mood Mood;
};

To partition a collection of Actor objects such that all friendly actors are separated from the others, you can define a predicate that checks the Mood of each Actor and use it with std::ranges::partition().

Here's an example:

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

enum class Mood { Friendly, Neutral, Hostile };

class Actor {/*...*/}; int main() { using enum Mood; std::vector<Actor> Actors{ {"Angry Goblin", Hostile}, {"Helpful Guard", Friendly}, {"Surly Goat", Neutral}, }; auto isFriendly = [](const Actor& A) { return A.Mood == Friendly; }; std::ranges::partition(Actors, isFriendly); std::cout << "After partitioning: "; for (const Actor& A : Actors) { std::cout << A.Name << ", "; } std::cout << "\n"; }
After partitioning: Helpful Guard, Angry Goblin, Surly Goat,

In this example, the isFriendly predicate checks if an Actor is friendly. The partition() function then rearranges the Actors vector so that all friendly actors are placed at the beginning.

Using partition() with custom data types is a powerful way to organize complex collections based on specific criteria defined by your predicates. Ensure your predicate function is well-defined to avoid unexpected behavior.

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