Your browser does not support our blog javascript

iterator

search for more blogs here

 
Home - Take this blog! - Get your Author's Pass Here - Submit Comments Below

Vector Iterator Help

Posted by ~Ray @ 2008-11-13 12:19:23


Hi,Im trying to remove an element from a vector inside a loop using an iterator which it seems I am doing incorrectly. Looking around it seems this fails (it gives a segmentation fault) because i am using an invalidated iterator but im not sure how to correct this?Thanks for any help. Jack A vector's iterators are invalidated when its memory is reallocated. Additionally inserting or deleting an element in the middle of a vector invalidates all iterators that point to elements following the insertion or deletion point. So use a container which is more friendly in this situation or stop accessing any invalidated iterator particularly bm showing the code of the whole loop would be helpful to answer your question in depth for example if you want to delete a number of bombs with a special property in a single loop this could help: erase returns an iterator that you should use to continue through the vector. A typical format for erasing while iterating looks like this: for(vector<bomb>::iterator bm = bombs begin(); bm!=bombs end(); ){ if (/* erase */) bm = bombs erase(bm); else ++bm;} Thanks the entire code below i would like to stick with the vector container if at all possible i have modified my loop/erase code to 'attempt' to re validate the iterator but it still doesnt work. for(vector<bomb>::iterator bm = bombs begin(); bm!=bombs end(); ){// Draw bomb glRectd(bm->x1 bm->y1 bm->x2 bm->y2); // Move bomb downards bm->y1-= delta_time * BOMB_VELOCITY; bm->y2-= delta_time * BOMB_VELOCITY; // Check for impact if((bm->x2 <= ship->RightX && bm->x1 >= ship->LeftX) && (bm->y2 <= ship->TopY && bm->y2 >= ship->LeftY)) {// Impact occuredbm = bombs erase(bm);// destroy bomb ship->ship_destroy = true; ship->lives--; }++bm;} This still segment faults :SThanks again You didn't quite follow my example. Notice that in my example. ++bm is called in the else. That means it isn't called if erase is called. In your code ++bm will always be called every time through the loop which is why it fails. You should only do one or the other. Either call erase and save the return value or increment with ++.[ADVERTHERE]Related article:
http://cboard.cprogramming.com/showthread.php?t=95390


0 Comments:


No comments have been posted yet!

From:   Website:
Subject:   Code:
Message:


   

 


 

 

 





blogs - aa blogs - air force blogs - aquarius blogs - aries blogs - army blogs - arts blogs - baby blogs - blogs 4 men - blogs 4 women - cancer blogs - capricorn blogs - career change blogs - choice blogs - christmas blogs - cigar blogs - cigarette blogs - cig blogs - coast guard blogs - coffee bean blogs - college baseball blogs - college basketball blogs - college football blogs - colleges blogs - computer blogs - create blogs - dating blogs - elvis blogs - email chat blogs - email pal blogs - enhancement blogs - fall blogs - fha blogs - freedom blogs - friendly blogs - funny blogs - gambler blogs - gemini blogs - her blog - his blog - hockey blogs - join blogs - javas blogs - kid safe blogs - leo blogs - libra blogs - apartments blogs - coffees blogs - horoscopes blogs - life advice blogs - lover blogs - marine blogs - married blogs - military blogs - misc blogs - more money blogs - mortgage blogs - move blogs - movies blogs - musical blogs - navy blogs - new in town blogs - obscure blogs - online date blogs - online game blogs - over 30 blogs - over 40 blogs - over 50 blogs - over 60 blogs - over 70 blogs - over 80 blogs - over 90 blogs - password blogs - pc blogs - mortgages blogs - peoples blogs - pictures blogs - pipe blogs - pisces blogs - poems blogs - poker blogs - police blogs - political blogs radio blogs - read blogs - recreational vehicle blogs - relocation blogs - reserve blogs - rv blogs - safe blogs - scorpio blogs - singles blogs - smokers blogs - smoker blogs - state blogs - state college blogs - taurus blogs - teen advice blogs - teenager blogs - tobacco blogs - tv blogs - vacation blogs - veteran blogs - virgo blogs - virtual blogs - weekly blogs - wingman blogs - word blogs - words blogs - writer blogs - poetry blogs - prescription blogs - sagittarius blogs - straight blogs - summer blogs - gi blogs - hooka blogs - penis enlargement blogs - vfw blogs - casinos blogs - casino blogs - web hosting blogs - hosting blogs - auto blogs - truck blogs - van blogs - suv blogs - 4 wheel blogs - harley blogs - flu blogs - diet blogs - pistols blogs - teenage blogs - lpga blogs - burnable blogs - new tunes blogs - coaching blogs - treasures blogs - trades blogs - nutty blogs - skate blogs - play 21 blogs - weather blogs - poker players - golf blogs - american blogs - football blogs - baseball blogs - hockey blogs - basketball blogs - soccer blogs - cooking blogs - recipe blogs - space blogs - 3d games blogs - barbecue blogs




blogs home