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.
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.
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.
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; }
| ~~~~^~~~~
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.
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.
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
}
operator< to the Person struct that orders people by their age in ascending order (youngest first).