简体   繁体   中英

Why c++ containers do not implement erase( reverse_iterator position )?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM