Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Deleting from vector with iterator (99% sure this is the way to go)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
22 replies to this topic

#21 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 21 July 2012 - 08:09 AM

Wierd no one thought to check if the vector wasnt empty before running that delete state...

a simple

if(!vector.empty())
{
do delete code stuff
}

It sounds like its trying to access a element that doesnt exist, and it never hurts ( at least I dont think it does ) to check if a container is empty before doing stuff.


But if the vector is empty the for loop wont run at all if i understand it right. And the code works now(same that i posted), i guess i had sloppy code somewhere else.

Sponsor:

#22 ASnogarD   Members   -  Reputation: 212

Like
0Likes
Like

Posted 21 July 2012 - 08:21 AM

Still a good idea to have that check, even if you are certain it wont call an empty vector... if you are concerned that it merely hides bad code you could add a error conditon so it notifies / logs the fact a call to the vector was made even though its empty.

if vector is not empty - do stuff - else - report attempt to access empty vector.

At least you will know that you were somehow accessing a empty vector, I use that a lot and it saves me quiet a lot of headaches.
I find a lot of time when programming I think something should do xyz, and never abc but due to silly faults what I get is abc, and as I have convinced myself the result cant be abc I cant work out why the code isnt working... if I set up my code to report back ( usually a text msg on screen, or a pass a msg to a vector I use to store msgs and display them in a console like function ), I can see what it is actually doing, rather than what I think it should be doing.

#23 dimitri.adamou   Members   -  Reputation: 329

Like
0Likes
Like

Posted 21 July 2012 - 06:13 PM

iterators will only point to the end if its empty but yeah the habbit of checking is good




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS