Algorithms use operator== and operator< by default

For comparison, an algorithm relies on the fundamental == and < operators, as in the case of an integer. To be able to use your own classes with algorithms, operator== and operator< must either be provided by the class or as an argument to the algorithm.

The following example shows these operators implemented in a simple Flower class, where operator== is utilized by std::find, and operator< is utilized by std::max_element.

struct Flower {  // Is equal operation, used when finding  auto operator==(const Flower& f) const {    return height_ == f.height_; }  // Is less than operation, used when sorting  auto operator<(const Flower& f) const {    return height_ < f.height_; }  int height_{};};auto garden ...

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.