3 ms·
Why are there no STL algorithms that work on whole containers?
- makecheck 15y agoI'm surprised he didn't give an even simpler reason, which is that algorithms were designed to work with pointers as well as objects. You can't call begin() or end() on a pointer. And although you can figure out the end of a stack-allocated C array, you can't do it for a heap-based array. For example, with "x = new int[30]" and a call like "sort(x, ...)", you may know the size of "x" but the algorithm can't know. The solution is to make algorithms accept a form like "sort(x, x + 30, ...)". Of course now, with things like std::array in the standard, there are fewer reasons to care about pointers than before.
- keveman 15y agoGood point. I haven't used STL algorithms on raw pointers in quite a while, that I didn't think of that reason at all.