Skip to main content
gamedev.net
🔒 Locked

[C++] vector order

Started by Headkaze 0 replies 1.5k views
Original Post
Headkaze
Headkaze
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?
Headkaze
Headkaze
I'm really sorry please delete this post.

I forgot I sort the m_pileArray later in the code!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.