• Advertisement

Archived

This topic is now archived and is closed to further replies.

What's wrong with my vector?

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

Hey, I have a problem with vector.. everything seems fine to me and I really don't understand what can cause the problem. My vector work correctly, but the problem occurs when I erase something from my vector... If I want to remove the second element, I will do this: int num = 1; m_Vector.erase(m_Vector.begin()+num); This works fine.. but when I erase something, immediatly after, I will enter this vector loop: for(int i=0; i Someone know what could be the cause of the problem? Thanks [edited by - Hedos on March 7, 2004 1:02:43 AM]

Share this post


Link to post
Share on other sites
Advertisement
quote:

m_Vector.erase(m_VectorGrass.begin()+num);


You''re erasing something from a different vector than the vector you got the iterator from?

Share this post


Link to post
Share on other sites
I invite you to post your actual code section from where you created the vector to where the crash occurs.

Share this post


Link to post
Share on other sites
quote:

You're erasing something from a different vector than the vector you got the iterator from?


Sorry, that was an mistake in my post, I edited it


Ok, so here is my actual code:

cGrass m_GrassDefault;
std::vector<cGrass> m_VectorGrass;
m_VectorGrass.reserve(1024);
m_VectorGrass.clear();
bool cWorld::GrassCreate(int tileNum)
{
m_GrassDefault.SetTileNum(tileNum);
m_GrassDefault.RandomizeDelay();
m_GrassDefault.RandomTile();
m_GrassDefault.SetLastDelay(timeGetTime());
m_VectorGrass.push_back(m_GrassDefault);
return true;
}
void cWorld::GrassRemove(int num)
{
m_VectorGrass.erase(m_VectorGrass.begin()+num);
}
void cWorld::GrassDoProcess()
{
for(int i=0; i<m_VectorGrass.size(); i++)
{
m_VectorGrass[i].DoProcess(); //Crash right here if I erased something

}
}

//Then I have my game loop

//GrassDoProcess() is called every frame

//I can add elements with mouse click

//I use m_World.GrassCreate(tileNum); to add an element

//Everything works fine, except if I try to remove an element

if(bRButton)
{
m_World.GrassRemove( someElement );
}
//After removing something, the game will crash in the GrassDoProcess loop




[edited by - Hedos on March 7, 2004 1:07:41 AM]

[edited by - Hedos on March 7, 2004 1:12:05 AM]

Share this post


Link to post
Share on other sites
hmm.... also show the code where you added an element to the vector and stored its iterator in someElement. (oh, and you should probably use [ source ] tags rather than [ code ] tags; they''re nicer looking and preserve indentation.)


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Well, think about it. When iterating through the vector, you grab an iterator for an element which is currently in the vector. Then, while you''re iterating, you remove the element. This means that the iterator is no longer part of the vector, so you can''t go to the next element in the vector. In short, you have to hold the value of the previous element, delete the current element, then increment the previous iterator.

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
hmm.... also show the code where you added an element to the vector and stored its iterator in someElement. (oh, and you should probably use [ source ] tags rather than [ code ] tags; they''re nicer looking and preserve indentation.)


"Sneftel is correct, if rather vulgar." --Flarelocke


I added the code where I add elements.. but what do you mean by iterator?
I''ve heard a little bit about iterators but I really don''t know what it is used for?

Share this post


Link to post
Share on other sites
quote:
Original post by psykr
Well, think about it. When iterating through the vector, you grab an iterator for an element which is currently in the vector. Then, while you''re iterating, you remove the element. This means that the iterator is no longer part of the vector, so you can''t go to the next element in the vector. In short, you have to hold the value of the previous element, delete the current element, then increment the previous iterator.


wrong. All that has to be done is assign the return from calling the erase() member back to the iterator that will set it in the next position following the deleted element.

Share this post


Link to post
Share on other sites
Oh well.. I think I got to read about iterators

Share this post


Link to post
Share on other sites
quote:
Original post by Hedos
Oh well.. I think I got to read about iterators


yes, its a good idea in general, however, I can''t seem to pick up on whats happening in your code. Specifically what is DoProcess() doing? What are the compiler errors saying? Perhaps others here might be seeing something with what code you posted that I having noticed.

Share this post


Link to post
Share on other sites
Here''s the standard algorithm for iterating through a vector and checking a condition for erasing the cell we''re currently looking at:


int i = 0;
while (i != MyVector.size())
{
if(CONDITION)
MyVector.erase(&MyVector[i]);

else // we only move i forward if we did not erase a cell because erasing a cell shifts the entire vector backwards

i++;
}

Share this post


Link to post
Share on other sites
quote:
Original post by Enokh
Here''s the standard algorithm for iterating through a vector and checking a condition for erasing the cell we''re currently looking at:



"The standard" algorithm for iterating through a vector doesn''t involve iterators? Ok...

Share this post


Link to post
Share on other sites
quote:
Original post by Enokh
Here''s the standard algorithm for iterating through a vector and checking a condition for erasing the cell we''re currently looking at:


int i = 0;
while (i != MyVector.size())
{
if(CONDITION)
MyVector.erase(&MyVector[i]);

else // we only move i forward if we did not erase a cell because erasing a cell shifts the entire vector backwards

i++;
}

That won''t even compile under the compiler I use. &MyVector[i] is not necessarily an iterator and should not be passed to erase.

Share this post


Link to post
Share on other sites

  • Advertisement