Jump to content

Decreasing an vector iterator


Laurens
 Share

Recommended Posts

Hi everyone,

 

I have a vector with objects in my project that is iterated over. Depending on the state of the object iterated over, I need to get a reference to the object it previously iterated over.

 

I have already tried i-- and (*(i--)) without result. Is this even possible? I would prefer it over just keeping a reference to the previous object in a seperate variable.

 

Thanks!

Link to comment
Share on other sites

If its a vector collection object then you could just address it directly using an index (I know some people scorn doing this and would always advise using a proper iterator but so long as you are sensible it works fine and you shouldn't run into problems, microsoft provided the functionality after all).

 

for(i=0; i < myVectorCollection.size(), i++)
{
   n = myVectorCollection[i]->someItem;
   if (n ==3) 
   {
        n2 = myVectorCollection[i-1]->someOtherItem;
   }
}

 

should work fine so long as i-1 is not less than 0

 

I'm surprised decrementing the iterator didn't work but I'm not sure I've ever tried that so I'll take your word for it!

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

I was indeed hesitant to address it using an index because I was under the impression this was widely regarded as a bad practice. Guess it can't do any harm for this particular part of my application so I'll use it anyway then. :)

 

Thanks!

Link to comment
Share on other sites

It is by purists, but I actually never use anything else with vectors and have never run into a problem. You obviously need to be aware of what you are doing and the bounds of the vector. A poor coder will make mistakes no matter what you do to try and prevent them in my experience :)

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

Disregard that, decrementing the iterator did work.

 

I was using this in a snippet of code that removed a GameScreen from a stack of screens. I removed the screen it was currently iterator over, then tried to get the previous one while there was only one screen left in the vector. :)

 

Sorry to have wasted your time though! :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...