The Spaceship Operator and Expression Rewriting

Using <=> for Sorting

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

Abstract art representing computer programming

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.

This Question is from the Lesson:

The Spaceship Operator and Expression Rewriting

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

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

The Spaceship Operator and Expression Rewriting

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

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved