Does this look correct?



 DEVELOP > c-Plus-Plus > Does this look correct?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "JustSomeGuy"
Date: 09 Mar 2006 04:31:11 PM
Object: Does this look correct?
I want to go through a std::list backwards....
std::list<mytype>::reverse_iterator it;
for (it=out.rbegin(); it != out.rend(); --it)
{
...
}
.

User: "Victor Bazarov"

Title: Re: Does this look correct? 09 Mar 2006 04:42:08 PM
JustSomeGuy wrote:

I want to go through a std::list backwards....

std::list<mytype>::reverse_iterator it;

for (it=out.rbegin(); it != out.rend(); --it)
{
...
}

Looks correct. The only thing is that if you modify the list in the
middle of iterating through it, the iterator _may_ become invalid. If
your '...' contain something like
out.erase(it);
then it's a mistake because 'it' is invalid after that. Also, there
seems to be no need to declare 'it' outside of the 'for'. Keep it tight:
for (std::list<mytype>::reverse_iterator it = out.rbegin(); ...
V
--
Please remove capital As from my address when replying by mail
.
User: "JustSomeGuy"

Title: Re: Does this look correct? 09 Mar 2006 04:48:57 PM
So the --it is correct... It isn't ++it (In the for loop control)
.
User: "Victor Bazarov"

Title: Re: Does this look correct? 09 Mar 2006 05:00:01 PM
JustSomeGuy wrote:

So the --it is correct... It isn't ++it (In the for loop control)

Oh... Hold on... You may be onto something... Gonna check...
.

User: "Phlip"

Title: Re: Does this look correct? 09 Mar 2006 04:52:29 PM
JustSomeGuy wrote:

So the --it is correct... It isn't ++it (In the for loop control)

Many iterators can implement as pointers, so...
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
.
User: "benben"

Title: Re: Does this look correct? 10 Mar 2006 12:04:29 AM
Phlip wrote:

JustSomeGuy wrote:

So the --it is correct... It isn't ++it (In the for loop control)


Many iterators can implement as pointers, so...

So what? Reverse iterators are never pointers, period.
Ben
.
User: "Phlip"

Title: Re: Does this look correct? 10 Mar 2006 12:34:31 AM
benben wrote:

Many iterators can implement as pointers, so...

So what? Reverse iterators are never pointers, period.

So thanks for completing my post; that's what.
One might thereby presume that bidirection iterators can, hence the _point_
of reverse iterators is they don't really, hence don't use rbegin() and
rend() on bidirectional iterators, etc.
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
.
User: "Andrew Koenig"

Title: Re: Does this look correct? 10 Mar 2006 09:20:03 AM
"Phlip" <phlipcpp@yahoo.com> wrote in message
news:X59Qf.21079$tb3.7707@newssvr24.news.prodigy.net...

One might thereby presume that bidirection iterators can, hence the
_point_ of reverse iterators is they don't really, hence don't use
rbegin() and rend() on bidirectional iterators, etc.

How's that again? You use rbegin and rend on containers, not iterators.
And if you apply rbegin to a list, what you get back is a bidirectional
iterator that traverses the list backward.
.
User: "Phlip"

Title: Re: Does this look correct? 10 Mar 2006 12:55:21 PM
Andrew Koenig wrote:

How's that again?

I was about to tell the OP that because some iterators may implement as
pointers, they should use -- to go backwards. Then I realized I didn't
actually know that. (Yes, there have been times I went and grabbed the
manual to answer a post. I declined this time because the thread was fresh
and I trust many^W a few people here to cover such things.)
Now I realize that the point of rbegin() and rend() is to supply iterators
that algorithms think are going forward when they are really going
backwards. Cute!
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
.
User: "JustSomeGuy"

Title: Re: Does this look correct? 12 Mar 2006 07:10:26 PM
Ok.. so now that I've started a fight....
The iterator to be used is ++ and not --????
Funny how a simple question about how to iterate through a list
backwards can be so hard to ask.
.
User: "Victor Bazarov"

Title: Re: Does this look correct? 12 Mar 2006 08:16:58 PM
JustSomeGuy wrote:

Ok.. so now that I've started a fight....
The iterator to be used is ++ and not --????
Funny how a simple question about how to iterate through a list
backwards can be so hard to ask.

You started what fight? The reverse iterator goes from the back of
the list to its beginning using ++. You can use a regular iterator
to do essentially the same, but then you'd need to use --.
V
--
Please remove capital As from my address when replying by mail
.







User: "Victor Bazarov"

Title: Re: Does this look correct? 09 Mar 2006 05:01:57 PM
JustSomeGuy wrote:

So the --it is correct... It isn't ++it (In the for loop control)

You're right! It should be ++.
V
--
Please remove capital As from my address when replying by mail
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER