Jump to content
  • Advertisement
Sign in to follow this  
51mon

Get access to members through iterators

This topic is 4239 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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!