Sign in to follow this  

[C++] vector order

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

[code]vector<Pile *> m_pileArray;[/code]

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

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

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.

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

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

[code]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[/code]

And later on I print it out again

[code]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[/code]

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

This topic is 2322 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this