Limitations of the iterators in STL

Although the iterator and algorithm concepts in STL have quite a few good properties, they lack composability.

Let's say, we have some sort of a Warrior class with an ability and a level of ability, as implemented below:

enum class EAbility { Fencing, Archery };class Warrior {public:  EAbility ability_{};  int level_{};  std::string name_{};};

Now, let's say we want to find the Warrior with the highest level of Archery in the list of warriors.

If we were to use STL, the algorithm we'd use is std::max_element(), operating on level_, but as we only want to take the warriors with the ability of Archery into account, it gets tricky. Essentially, we want to compose a new algorithm out of a combination of std::copy_if() ...

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.