Laurens Posted June 4, 2010 Share Posted June 4, 2010 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! Quote Link to comment Share on other sites More sharing options...
Pixel Perfect Posted June 4, 2010 Share Posted June 4, 2010 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! Quote 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 More sharing options...
Laurens Posted June 4, 2010 Author Share Posted June 4, 2010 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! Quote Link to comment Share on other sites More sharing options...
Pixel Perfect Posted June 4, 2010 Share Posted June 4, 2010 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 Quote 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 More sharing options...
Laurens Posted June 4, 2010 Author Share Posted June 4, 2010 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! Quote Link to comment Share on other sites More sharing options...
Pixel Perfect Posted June 4, 2010 Share Posted June 4, 2010 No worries, I though it should have just hadn't actually tried that before! Quote 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 More sharing options...
Recommended Posts
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.