What are the shortcomings of std::reverse_iterator?
The documentation for boost's spec开发者_如何学Cialized iterator adaptors states that boost::reverse_iterator
"Corrects many of the shortcomings of C++98's std::reverse_iterator."
What are these shortcomings? I can't seem to find a description of these shortcomings.
FOLLOW-UP QUESTION:
How does boost::reverse_iterator correct these shortcomings?
Well, the big problem is that they're not forward iterators, and there's stuff that pretty much expect forward iterators. So, you have to do some funny conversions to get things to work. To name some issues
Some versions of
erase()
andinsert()
require iterators rather than reverse iterators. That means that if you're using a reverse iterators and you want toinsert()
orerase()
, you're going to have to use the reverse iterator'sbase()
function to get your hands on a forward iterator. There is no automatic conversion.base()
returns the forward iterator equivalent to the reverse iterator in terms of insertion. That is, insert inserts in front of the current element. The element that the reverse iterator is pointing at, therefore, would be the wrong element to be pointing at ifbase()
gave you an iterator that pointed at the same element. So, it points one forward, and you can use it for insertion.Because
base()
returns an iterator pointing at a different element, it's the wrong element to use forerase()
. If you callederase()
on the iterator frombase()
, you'd erase one element forward in the container from the element that the reverse iterator points at, so you have to increment the reverse iterator before callingbase()
in order to get the correct forward iterator to use forerase()
.Whether you can even use
base()
witherase()
to correctly erase an element depends entirely on your implementation. It works with gcc, but with Visual Studio they're really just wrapping a forward iterator in a manner that makes it so that it doesn't work to useerase()
when dealing with reverse iterators and Visual Studio. I don't recall whetherinsert()
has the same problem, but reverse iterators don't work the same between different implementations of C++ (according to the Visual Studio guys, the standard wasn't clear enough), so it can be kind of hairy to use them for anything other than simply iterating over a container.
There are probably other issues as well, but dealing with any type of iterator other than a non-const, forward iterator in C++ when doing anything other than simply iterating over a container can get a bit hairy - if you can even do it all - because so many functions require non-const forward iterators rather than any other kind of iterator.
If you really want to know the differences between the various iterator types and the issues associated with them, I recommend reading Scott Meyer's Effective STL. It has a great chapter on iterators.
EDIT: As for how Boost's reverse iterator corrects those shortcomings, I'm afraid that I don't have a clue. I'm aware of some of the standard reverse iterator's shortcomings and have been bitten by them in the past, but I've never used Boost much, so I'm not familiar with their reverse iterators at all. Sorry.
精彩评论