Hey guys, I'm trying to delete an element of a vector with an iterator using the erase function. Here's the basics of my way of trying to delete it.
In my startup code I'm creating the vector and iterator like this. Yes it does have to be like this as it's for an assessment.
1 2 3 4 5 6
//Create a vector that holds 20 Knight Objects from the structure
std::vector<Knight> *myKnightsOne = new std::vector<Knight>(5, Knight());
std::vector<Knight> *myKnightsTwo = new std::vector<Knight>(5, Knight());
//Create an iterator that can travel through the vector to pull each Knight Object out
std::vector<Knight>::iterator *myKnightsOneIterator = new std::vector<Knight>::iterator;
std::vector<Knight>::iterator *myKnightsTwoIterator = new std::vector<Knight>::iterator;
In the startup above when i call this function - along with the player movement functions. This code activates itself when 2 of the vector elements touch.
Now the fight between them is normal - they hit each other until one of them has health thats less than 0. Only problem is when that happens I get an error about insertion errors.
The code just above this line is where I'm having the problem trying to delete the element from the vector. I've tried the two IF statements in different spots inside this function. But they all end up with errors.
I've tried also using
pVecOne->erase(*pItOne);
But that gives me pretty much the same problem
So my question is how do I remove the element and where should i put this?
Ok now I'm confused - this new code I used sometimes works - it'll display the quick fight and delete the element. But sometimes it won't. It'll just give me the press any key - yet for some random ones the fight will happen, the element will delete, and it won't show up after that?
So basically I can't do this? Ok so say if I was to still use vectors is there a way to make it possible at all? Or should i rewrite the code and use a list or something? - Linked list, Binary Tree?
You could use a std::list, and then you have to do 3 things:
1) Remove the increment of the iterator in both for() loops;
2) In the cases where you erase an iterator, you must first
get an iterator to the next element, then erase the one
you want to erase, and finally re-assign pItOne or pItTwo
with the temporary iterator;
3) In the cases where you don't erase an iterator, you have
to increment the iterator (since you did 1, nothing will
increment the iterator otherwise).
I've just worked out if I'm using this code without the erasing an element in the vector.. After a couple of fights or sometimes on the first fight - It'll just shut down anyway and tell me to "Press any key to continue"
So something is wrong with this code in general then?
Well if anyone knows if it's possible to do it like this exactly how to lay it out - if its possible for someone to give me a small demo of how it works. Please let me know. I'm heading out for a while and will be back soon. Cheers guys!
Ok, well I'm wondering why you are passing pointers to iterators...there is no reason to IMO...but since you said they are requiring you to, I guess you don't have a choice. As for the random exiting error...have you tried stepping through it with a debugger? I don't really see why it would randomly be quitting though...what is the movement/fight calling code?