Connecting Tech Pros Worldwide Forums | Help | Site Map

-- myList.begin()

earthwormgaz
Guest
 
Posts: n/a
#1: Oct 20 '06
Is that operation legal does anyone know?

Can I legally compare an iterator to the thing just before begin to see
if decrementing my iterator has taken it past the workable range of the
list?

If not, what should I be doing instead to terminate this loop ...

while(-- it != /* hmm */)
{
// play with it->
}


Gavin Deane
Guest
 
Posts: n/a
#2: Oct 20 '06

re: -- myList.begin()



earthwormgaz wrote:
Quote:
Is that operation legal does anyone know?
>
Can I legally compare an iterator to the thing just before begin to see
if decrementing my iterator has taken it past the workable range of the
list?
No. That would be undefined behaviour.
Quote:
If not, what should I be doing instead to terminate this loop ...
>
while(-- it != /* hmm */)
{
// play with it->
}
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.

Gavin Deane

earthwormgaz
Guest
 
Posts: n/a
#3: Oct 20 '06

re: -- myList.begin()



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

Rolf Magnus
Guest
 
Posts: n/a
#4: Oct 20 '06

re: -- myList.begin()


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.
list iterators are not random access. They are only bidirectional iterators.
Quote:
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.
What's wrong with reverse iterators? Use rbegin()/rend() to go through the
container backwards.

Clark S. Cox III
Guest
 
Posts: n/a
#5: Oct 20 '06

re: -- myList.begin()


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.
No, it's not. List does not have random access iterators.
Quote:
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.
Did you read Gavin Deane's post? Use reverse iterators.


--
Clark S. Cox III
clarkcox3@gmail.com
earthwormgaz
Guest
 
Posts: n/a
#6: Oct 20 '06

re: -- myList.begin()



Rolf Magnus wrote:
Quote:
What's wrong with reverse iterators? Use rbegin()/rend() to go through the
container backwards.
Nowt, but I am writing a bi-directional iterator for what amounts to
this ...

class Tree
{
std::list<Treem_lsChildren;
};

So, my iterator type contains:

class TreeIterator
{
Tree * m_pTree;
std::list<Tree>::iterator m_pIt;
};

That means in dealing with the -- operator I have to deal with m_pIt
going past begin(). I appear to have a working algorithm now anyway.

/me waits for somebody to tell him Boost or something already does all
this :)

Cheers folks,

Gaz

Salt_Peter
Guest
 
Posts: n/a
#7: Oct 20 '06

re: -- myList.begin()



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.

Closed Thread