Skip to content

Latest commit

 

History

History
26 lines (22 loc) · 826 Bytes

overload_less.md

File metadata and controls

26 lines (22 loc) · 826 Bytes

Overloading the Less Than Operator

The less than operator returns true iff one object less than the other. If it establishes a strict weak ordering, it makes a type LessThanComparable, which allows using the type in a std::map, in std::sort and other algorithms.

Example

struct point {
    int x, y;
    // explicit defaulting since C++20
    bool operator>(const point&) const = default;
};
// can also be defined outside the class
bool operator<(point a, point b) {
    return a.x < b.x ||
           a.x == b.x && a.y < b.y;
}

See Also

<:cppreference:875716540929015908> Comparison Operators