Item 20. STL Function Objects

How did we ever get by without the STL? Not only is it easier and faster to write complex code, but that code is both standard and highly optimized.

std::vector<std::string> names;//...std::sort( names.begin(), names.end() );

Another nice thing about the STL is that it’s highly configurable. In the code above, we used string’s less-than operator to sort a vector of strings, but we don’t always have a less-than operator to work with, or we may not want to sort in ascending order.

image

The State class (which represents a state of the union) doesn’t have a less-than operator, and we probably don’t want to implement one ...

Get C++ Common Knowledge: Essential Intermediate Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.