Archived

This topic is now archived and is closed to further replies.

Why use reverse iterators?

This topic is 5498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was working with iterators, and trying to traverse a list backwards. I wasn''t quite doing it right, but I know what I was doing wrong. Then I remembered about reverse_iterator, so I changed my code to use reverse_iterators to fix my problem. My question is: Is there any real reason to use reverse_iterator instead of using a forward iterator and using --i?

Share this post


Link to post
Share on other sites
The re''s another important reason to use reverse iterators:

begin <-> first
rbegin <-> last

end <-> ++last
rend <-> --begin


The initial and terminating positions are different! (the insidious off-by-1 error).


PS: -- is not a valid operation on a forward iterator (hence the name, forward )



- Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
[Free C Libraries | zlib ]

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
The initial and terminating positions are different! (the insidious off-by-1 error).

Right. That was the problem I had and the reason I went to reverse iterators. But you can still account for that, and iterate properly.

quote:

PS: -- is not a valid operation on a forward iterator (hence the name, forward )


Ah, but if you have a forward iterator, doesn''t that mean you can''t have a reverse_iterator?

Share this post


Link to post
Share on other sites
quote:
Original post by jediknight219
Ah, but if you have a forward iterator, doesn''t that mean you can''t have a reverse_iterator?


No. It only means you do not have a bidirectional iterator

Share this post


Link to post
Share on other sites
quote:
Original post by jediknight219
Ah, but if you have a forward iterator, doesn''t that mean you can''t have a reverse_iterator?


This is what I meant. Check it out at http://www.sgi.com/tech/stl/ReversibleContainer.html

Reversible Containers:
"Two new types are introduced. In addition, the iterator type and the const iterator type must satisfy a more stringent requirement than for a Forward Container. The iterator and reverse iterator types must be Bidirectional Iterators, not merely Forward Iterators"

So unless I''m reading this incorrectly, if you have a forward iterator, you''re not using a reversible container, and therefore can''t have a reverse iterator.

Share this post


Link to post
Share on other sites
quote:
Original post by jediknight219
So unless I''m reading this incorrectly, if you have a forward iterator, you''re not using a reversible container, and therefore can''t have a reverse iterator.

Yes, you''re correct. There''s nothing that says a forward iterator does not support operator--, or a reverse iterator does not support operator++. The difference is in what those operators mean for each type of iterator. The name "forward" does not come about from not supporting operator--, it comes from the direction it traverses: i.e. forwards.

Share this post


Link to post
Share on other sites
To answer the original question: you sometimes have to use a reverse iterator if you are copying data and your destination overlaps your source.

Cédric

Share this post


Link to post
Share on other sites
quote:

PS: -- is not a valid operation on a forward iterator (hence the name, forward )



Egad, I only just now realized what you were saying. I''ve got a horrible mistake in my first post. That last sentence should have been something like:

My question is: Is there any real reason to use list::reverse_iterator instead of using list::iterator and using --i?

Share this post


Link to post
Share on other sites
quote:
Original post by Stoffel
Yes. That question was answered in the first response. Ponder DrPizza''s writing deeply, come back if you have any questions.


You are starting to sound like the Guru of Herb Sutter''s conversations

Share this post


Link to post
Share on other sites