Why do I have to always specify the range in STL’s algorithm functions explicitly, even if I want to work on the whole container?

Most of the time, the standard library is designed to provide the minimal interface necessary to accomplish all the tasks required, i.e. it tries to avoid interface bloat. You can operate on a whole container when the algorithm accepts a pair of iterators, but you could not operate on a subrange if the algorithm accepted a container. So the iterator pair is more fundamental, and so that’s what the standard library provides. Convenience functions are usually not included.

However, you’re certainly not the first person to think this way, and there’s the entire Boost.Range library devoted to treating a range (both a container and an arbitrary range) as a single entity instead of a pair of iterators.

There is also a formal proposal to incorporate Eric Niebler’s range library in a future version of the C++ standard library.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)