• Advertisement
Sign in to follow this  

Get access to members through iterators

This topic is 4142 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

If I got an iterator that points into a vector that contains pointers. Like this one: std::vector<MY_STRUCT*>::iterator itrThis; How do I get access to MY_STRUCTs member variables. The only way I could think of was this MY_STRUCT* pThat = *itrThis; pThat->fWhatever = 1.0f; But can I get access to the members directly through the iterator?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Agony
(*itrThis)->fWhatever

Thanks that wasn't to hard :)
But I have another question about iterators to. I need to step back in a vector so I was thinking of using a reverse iterator, I declare it like this:
std::vector<SCATTERING_LIGHT*>::reverse_iterator rev_itrLine;

But that doesn't seams to work, I got error messages when trying to assign values to it and no intellisense. The regular iterator works fine.
std::vector<SCATTERING_LIGHT*>::iterator itrLine;

This for example doesn't work.
rev_itrLine = itrLine;

[Edited by - 51mon on October 17, 2006 4:14:24 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
How (and what) are you trying to assign to the reverse_iterator variable?


I just want to step backward through the vector. I thought that a reverse_iterator would be nice to use but maybe it's not a good idea?

This is a piece of my code
std::vector<SCATTERING_LIGHT*> vecOldLine;
std::vector<SCATTERING_LIGHT*>::iterator itrThis;
std::vector<SCATTERING_LIGHT*>::reverse_iterator rev_itrLine;
for(int i=0, rev_itrLine = itrThis; i<5 && rev_itrLine!=vecOldLine.rend(); i++, rev_itrLine++)
{
...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by 51mon
Quote:
Original post by Zahlman
How (and what) are you trying to assign to the reverse_iterator variable?


I just want to step backward through the vector. I thought that a reverse_iterator would be nice to use but maybe it's not a good idea?

This is a piece of my code
std::vector<SCATTERING_LIGHT*> vecOldLine;
std::vector<SCATTERING_LIGHT*>::iterator itrThis;
std::vector<SCATTERING_LIGHT*>::reverse_iterator rev_itrLine;
for(int i=0, rev_itrLine = itrThis; i<5 && rev_itrLine!=vecOldLine.rend(); i++, rev_itrLine++)
{
...


I don't use reverse_iterators a lot, but I think you want this:

[source lang="C++]

std::vector<MyClass*> myvec;
std::vector<MyClass*>::reverse_itor ritor;

for(ritor=myvec.rbegin();ritor!=rend();++ritor) {
// Dereference ritor and do stuff with it.
}





Share this post


Link to post
Share on other sites
Yeah, you can't assign iterators to reverse_iterators.... they are different types.

Share this post


Link to post
Share on other sites
Heh, that anonymous post was me again. I always seem to do that, especially when I make a typo and then can't correct it.

Share this post


Link to post
Share on other sites
Quote:
Original post by phil_t
Yeah, you can't assign iterators to reverse_iterators.... they are different types.


OK I see. Is there any type that works for both iterators and reverse_iterators or any cast between them

Share this post


Link to post
Share on other sites
I've never tried it, but vector's iterator is supposed to be a random access iterator... you should be able to do both ++ and -- with it, no?

Share this post


Link to post
Share on other sites
Quote:
Original post by phil_t
I've never tried it, but vector's iterator is supposed to be a random access iterator... you should be able to do both ++ and -- with it, no?

Yeah, the only thing is that I can't stop at the first element when traversing backwards. Since end() returns a pointer beyond the last element but begin() at the first. I can solve this by handling my own dummy.

Share this post


Link to post
Share on other sites
Well, if you're *just* traversing backwards, it's no problem... just use reverse_iterator, rbegin() and rend().

Share this post


Link to post
Share on other sites
IIRC, you can construct a reverse_iterator from a corresponding iterator explicitly, and you can convert a reverse_iterator back into a normal iterator with .base() (you'll probably want to check the exact semantics on that one, though).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement