Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

[C++] vector order

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

For a card game I have a vector defined in a superclass as follows

vector<Pile *> m_pileArray;

For loading and saving reasons I need to be able to get the id (index) of the pile from the array.

I made the following methods for this reason

int GetPileId(Pile *pile) { for(int i=0; i<m_pileArray.size(); i++) if(m_pileArray == pile) return i; return -1; }
Pile *GetPileAddr(int id) { if(id == -1) return NULL; return m_pileArray[id]; }


For some strange reason I am getting id's that do not match those when the array is first created. There order has mysteriously changed. I have never experienced this before with a vector. I added the following method.

void ListPiles() { for(int i=0; i<m_pileArray.size(); i++) DEBUGLOG("%02d %08x", i, m_pileArray); }

Just to print out the addresses. Here is the output when I first create it

00 02278e04
01 02278e3c
02 02278e74
03 02278eac
04 02278ee4
05 02278f1c
06 02278f54
07 02278f8c
08 02278fc4
09 02278ffc
10 02279034
11 0227906c
12 022790a4


And later on I print it out again

00 02278eac
01 02278e74
02 02278e04
03 02278e3c
04 02278ee4
05 02278f1c
06 02278f54
07 02278f8c
08 02278fc4
09 02278ffc
10 02279034
11 0227906c
12 022790a4


What the? The order has mysteriously changed. After its first creation I never modify the vector. How can this happen?

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!