earthwormgaz wrote:
Quote:
Gavin Deane wrote:
Quote:
No. That would be undefined behaviour.
>
Right, I'll stop doing that then. It actually has worked for me, but is
clearly an unsafe thing to do, so that is the end of it.
>
Quote:
If you are using a container with bidirectional or random access
iterators (you mention the word list above and std::list has
bidirectional iterators) then look up the reverse iterators rbegin()
and rend(). They allow you to iterate backwards through the container.
It's not absolutely clear from your question, but it sounds like they
might be what you need.
>
I am using a list, so yes, random access iterator. It would seem that
all I can do is ...
>
-- it != myList.begin()
>
I'll have to rework a couple of algorithms for that I suppose.
>
Gaz
A container with bidirerctional iterators has 2 ends, one end gowing
forward indicating one past the last element and a rend gowing backward
indicating one past the first element.
#include <iostream>
#include <list>
int main()
{
std::list<intnlist;
for(int i = 0; i < 5; ++i)
{
nlist.push_back(i); // 0,1,2,3,4
}
typedef std::list<int>::reverse_iterator RIter;
for(RIter riter = nlist.rbegin(); riter != nlist.rend(); ++riter)
{
std::cout << *riter << std::endl;
}
return 0;
}
/*
4
3
2
1
0
*/
Note how incrementing (op++) a reverse iterator is used to displace it.