Question:
C++ vector data structure?
Ash
2010-08-18 19:27:50 UTC
I'm trying to delete an object from a vector at a specific position. I have a function that detects when the user clicks on an object. I iterate through the vector and check every object on the screen for a collision. This is where i'm unsure. I tried using the erase function but it crashes. Here's what the code looks like.

//it is my iterator and list is my vector
for ( it = list.begin(); it != list.end(); ++it ) {
if(it->checkHit(mouseCoords.x,mouseCoords.y))
list.erase(it);
}
Four answers:
Cubbi
2010-08-18 20:02:39 UTC
The error in this code:



for ( it = list.begin(); it != list.end(); ++it ) {

     if(it->checkHit(mouseCoords.x,mouseCoord…

         list.erase(it);

}



is that you ignore the return value of erase(it). Learn to never do that! The call to erase() invalidates your iterator "it" and any further access using it may (and most likely will) cause a crash. What you should do is write



     it = list.erase(it);



because erase() returns a valid iterator at the next item in the vector, which you can safely continue incrementing and dereferencing.



And since then your next ++it would then skip over the element right after the one erased, the better loop is



for ( it = list.begin(); it != list.end(); ) {

     if(it->checkHit(mouseCoords.x,mouseCoord…

         it=list.erase(it);

     else

         ++it;

}



or, to make this look even more like C++, rewrite this as a single call to list.erase(remove_if(list.begin(), list.end(), functor), list.end());, where functor is a function or a function object that executes your checkHit.



Also, note that if you're erasing from the middle of a vector a lot, a different standard container might be more efficient, depending on what other operations you're doing to it: either std::list or one of the associative containers.
2016-12-29 12:21:58 UTC
Your questions are incredibly no longer client-friendly to respond to and that's clearly a homework undertaking. So I won't furnish each and each little thing away. at first, working with information from c++ may be extremely complicated for the nice and comfortable programmer. i might first write some small attempt instructions to verify which you be responsive to undertaking-loose report io. IO in c++ is working equipment self keeping. So it incredibly isn't client-friendly to enable you to be responsive to precisely what to hunt for for by ability of using fact i do no longer comprehend what working equipment you're working with. yet often it variety of feels that fopen and fscanf and fprintf are the applications i might use again to a variety the matters indexed perfect right here. there are diverse components that coach you the thank you to coach them. Secondly, programmers attempt to "see there applications" in any diverse case. to respond to the question what applications do you go with ask your self what might I do if i replaced into doing this and not using a working laptop or laptop. possibly you will examine the enter, then calculate the output and then checklist the output. so your applications may be examine, Calculate, and checklist. education structures are complicated and it incredibly isn't client-friendly to enable you to be responsive to what to do by ability of using fact i do no longer comprehend what you should use. yet i might use a vector from the broadly used template library. this would perhaps be considered cheating for a homework undertaking. finally, injury your undertaking down into small chucks. come to a variety each of them and them composite the end result. do no longer attempt to do each and each little thing at as immediately as. Create and take a determine out each area independently. think of of of it as 5 small initiatives and not one massive one. sturdy fulfillment and bear in suggestions this could be form of relaxing, if it incredibly isn't possibly programming isn't for you because of the fact it incredibly is going to be a lot greater beneficial of an comparable :)
Kevin L
2010-08-18 20:05:21 UTC
You shouldn't be modifying the list while iterating through it. Try something like this:



Object *deleteObj = NULL;

for ( it = list.begin(); it != list.end(); ++it ) {

if(it->checkHit(mouseCoords.x,mouseCoord…

break;

}

list.erase(it);



..and if you need to continue checking the list after a delete, you should start from the beginning
santoro
2016-12-26 20:31:05 UTC
Your questions are somewhat no longer customer-friendly to respond to and that's clearly a homework difficulty. So I won't grant each and each and each little thing away. at first, working with information from c++ would be quite no longer basic for the warm programmer. i'd first write some small try instructions to be sure which you recognize difficulty-loose record io. IO in c++ is working equipment self sufficient. So that isn't customer-friendly to make it easier to recognize precisely what to seek for for by potential of fact i do no longer understand what working equipment you're working with. yet frequently it form of feels that fopen and fscanf and fprintf are the needs i'd use to compliment the topics indexed suitable here. there are dissimilar sources that coach you the thank you to be conscious them. Secondly, programmers attempt to "see there purposes" in any diverse case. to respond to the question what purposes do you like ask your self what would I do if i replaced into doing this and not applying a working workstation or computing gadget. possibly you will learn the enter, then calculate the output and then checklist the output. so your purposes would be learn, Calculate, and checklist. tips structures are complicated and that isn't customer-friendly to make it easier to recognize what to do by potential of fact i do no longer understand what you could desire to apply. yet i'd use a vector from the quite used template library. this would perhaps be considered cheating for a homework undertaking. ultimately, injury your difficulty down into small chucks. pick each and each of them and them composite the top effect. do no longer attempt to do each and each and each little thing at as right now as. Create and take a be sure out each and each area independently. think of of of it as 5 small projects and not one super one. sturdy success and undergo in concepts this would be type of relaxing, if that isn't possibly programming isn't for you as a results of fact it somewhat is going to be plenty greater of an comparable :)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...