7

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?

Community
  • 1
  • 1
VL-80
  • 517
  • 13
  • 29
  • Adding variant for reverse_iterator for all methods (why just erase ?) which take iterator would just increase the number of public methods for no gain (and some loose as so there will have more overload and user will have to avoid ambiguous calls). – Jarod42 Aug 02 '15 at 18:37

1 Answers1

4

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765