Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Iterators in reverse direction?

0 views
Skip to first unread message

Ramon F Herrera

unread,
Oct 13, 2009, 9:36:49 AM10/13/09
to

I need to have the option of outputting the data contained in a list
in a forward or in reverse fashion.

The forward iteration is easy enough:

for (list<string>::iterator itr = mylist.begin(); itr != mylist.end();
itr++)

I naively tried to reverse it like this:

for (list<string>::iterator itr = mylist.end(); itr != mylist.begin();
itr--)

But then I realized that the issue is not that simple, as you are not
supposed to find anything usable at the list.end().

Should I try to reverse:

- The iterator?
- The list itself?

TIA,

-Ramon

Helge Kruse

unread,
Oct 13, 2009, 9:56:43 AM10/13/09
to
Did you try

for (list<string>::reverse_iterator itr = mylist.rbegin(); itr !=
mylist.rend(); itr++) ?


Helge


"Ramon F Herrera" <ra...@conexus.net> wrote in message
news:6a123b5f-20e9-4e70...@a21g2000yqc.googlegroups.com...

Ramon F Herrera

unread,
Oct 13, 2009, 10:14:53 AM10/13/09
to
On Oct 13, 9:56 am, "Helge Kruse" <Helge.Kruse-nos...@gmx.net> wrote:
> Did you try
>
> for (list<string>::reverse_iterator itr = mylist.rbegin(); itr !=
> mylist.rend(); itr++) ?
>


Ah! That is the solution - I have seen those r-members around...

Thanks!

-Ramon

0 new messages