Why c++ containers do not implement erase( reverse

I looked at some C++ containers (vector, deque, list, map, set) and found that none of them implement

erase(reverse_iterator position)

There is a way to get iterator from reverse_iterator as described in this answer.

But why above containers do not implement the erase member function with reverse_iterator parameter?

Is there any significant difference between iterator and reverse_iterator which makes such implementation difficult or it was not implemented for another reason?


The same question can be asked about virtually any container function: why isn't it implemented for reverse iterators? The answer is probably the simple fact that reverse iterator is easily convertible to regular iterator through base() call. It makes more sense to place the burden of calling base() on the user instead of almost doubling the number of container's functions by implementing a "reverse" version for every single one of them.

One can argue that it breaks the "generality" of containers in external contexts that process these containers through reverse iterators. But from the very beginning reverse iterators were designed as "different" and not necessarily compatible with normal iterators in all but most trivial contexts.

链接地址: http://www.djcxy.com/p/87362.html

上一篇: 为什么HTML认为“chucknorris”是一种颜色?

下一篇: 为什么c ++容器不能执行擦除(反向)