Get access to members through iterators

Started by
11 comments, last by Zahlman 17 years, 6 months ago
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?
Advertisement
(*itrThis)->fWhatever
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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]
How (and what) are you trying to assign to the reverse_iterator variable?
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++)
{
...
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.}


Yeah, you can't assign iterators to reverse_iterators.... they are different types.
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.
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
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?

This topic is closed to new replies.

Advertisement