Skip to main content

Section 21.5 Sets and Comparisons

How does a set<string> keep the strings it contains in sorted order? It relies on being able to ask the strings to compare themselves using the < operator. The set container uses this operator to determine the order of the elements. Since the string class defines the < operator to do lexicographical (alphabetical) comparison, the set can use it to keep the strings in order.
That means to use a set with a custom type, that type must define the < operator so the set can order its elements. Below is a simple example of using a set to store a collection of Person structs. Currently, the Person struct does not define the < operator (it is commented out). Try running the program as is.
Listing 21.5.1.
The error message you get when you run the code without a operator< will likely look like a wall of text that can be difficult to understand. But if read down a ways you should see something like:
...error: no match for ‘operator<’ (operand types are ‘const Person’ and ‘const Person’)
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
This is the compiler telling us that it cannot find a way to compare two Person objects using the < operator, which it requires to store them in the set. To make this code work, we need to define the operator< for the Person struct. This next version of the program provides that operator.

Note 21.5.1.

Again, we are using a struct instead of a class to keep the example simple. We can define operators for structs just like we can for classes.
Listing 21.5.2.
The operator< defined in the Person struct allows one person, (A) to be compared to another person (B) by asking A < B. “Less than” here does not really mean less, it means “comes before”. The operator should return true if the object executing the code should come before other.
What if we want to sort the people by their age instead so that the oldest person comes first? To do that, we would need to change the implementation of the operator< to compare the age members instead of the name members. To make older people come first, we would want to return true if the current person’s age is greater than the other person’s age.
bool operator<(const Person& other) const {
  return age > other.age; // Order by age, oldest first
}
You can try this out. Replace the operator overload in Listing 21.5.2 with this new version. The program should then display the people from oldest to youngest.

Checkpoint 21.5.1.

Add an operator< to the Person struct that orders people by their age in ascending order (youngest first).
You have attempted of activities on this page.