The future of STL and the ranges library
This section deals with a library that is proposed for the upcoming C++ standard, C++20. As of now, it is available at https://github.com/ericniebler/range-v3. Compiling these examples will require an updated compiler.
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...