Using <=> for Sorting

Can <=> be used for sorting algorithms in C++20?

Yes, the <=> (spaceship) operator can be used for sorting algorithms in C++20. The spaceship operator allows you to define a single comparison function that can be used by sorting algorithms to determine the order of elements.

How It Works

When you define the <=> operator for your custom type, you provide a way to compare two instances of that type. Sorting algorithms, like std::sort(), can then use this comparison logic to sort a collection of your custom objects.

Example

Let's consider a Player class where we want to sort players by their scores using the spaceship operator:

#include <iostream>
#include <vector>
#include <algorithm>
#include <compare>
#include <string>

class Player {
public:
  Player(std::string name, int score)
    : Name{name}, Score{score} {}

  std::strong_ordering operator<=>(
    const Player& Other) const {
    return Score <=> Other.Score;
  }

  std::string Name;
  int Score;
};

int main() {
  std::vector<Player> players{
    {"Alice", 20}, {"Bob", 15}, {"Charlie", 25}
  };

  std::sort(players.begin(), players.end());

  for (const auto& player : players) {
    std::cout << player.Name
      << " - " << player.Score << "\n";
  }
}
Bob - 15
Alice - 20
Charlie - 25

Explanation

In this example, we define the <=> operator for the Player class to compare players by their scores. When we call std::sort(), it uses this comparison logic to sort the players in ascending order of their scores.

Benefits

  • Consistency: Defining the <=> operator ensures that all comparisons are consistent across your program.
  • Simplicity: You write less code, as you only need to define one comparison operator.
  • Flexibility: The same comparison logic can be reused in different contexts, such as sorting, searching, and other algorithms.

Conclusion

Using the <=> operator with sorting algorithms in C++20 simplifies your code and ensures consistency in how your custom types are compared. It leverages the power of the spaceship operator to provide a clear and concise way to define sorting logic.

The Spaceship Operator and Expression Rewriting

A guide to simplifying our comparison operators using C++20 features

Questions & Answers

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

Defining the == Operator
Why do we need to explicitly define the == operator if we already have <=>?
Equality vs. Equivalence
What are the differences between std::strong_ordering::equal and std::strong_ordering::equivalent?
Benefits of the Spaceship Operator
What are the benefits of using the spaceship operator <=> over traditional comparison operators?
Custom Comparison Logic
How can we implement custom comparison logic using the spaceship operator <=>?
Niche Cases for Three-Way Comparison
How does the C++20 standard handle types that do not naturally fit into the three-way comparison model?
Real-World Examples of <=>
What are some practical examples of using <=> in real-world applications?
Ask Your Own Question
Temporarily unavailable while we roll out updates. Back in a few days!