Go Back   Flash Flash Revolution > General Discussion > Technology
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
Old 03-4-2013, 07:46 PM   #1
d4u7211
Aficionado of Awk
FFR Veteran
 
d4u7211's Avatar
 
Join Date: Oct 2006
Location: Wherever my computer is
Age: 30
Posts: 1,276
Send a message via Skype™ to d4u7211
Default C++ iterator question

using the following code:

list<string>::iterator iter;
for (iter=names.begin(); iter!=names.end(); iter++) {
names.erase(iter);
}

we are supposed to assume that the program compiles but does not execute properly (logical error causing a CRASH). The list holds 20 names. What is wrong, I don't see anything wrong with it (but I'm sort of new on the whole pointer thing)?

names.end() points to the imaginary space after the last element in the list. begin() points at the first element of the list.

my two guesses is that either:
iter!=names.end() should be something else, or
names.erase(iter) should be names.erase(*iter);
__________________
Hardpain of Hell RELEASED!!!

d4u7211 is offline   Reply With Quote
Old 03-4-2013, 08:02 PM   #2
FissionMailed1
FFR Player
 
FissionMailed1's Avatar
 
Join Date: Feb 2012
Location: Massachusetts
Age: 33
Posts: 1,267
Send a message via Skype™ to FissionMailed1
Default Re: C++ iterator question

When you erase from std::list, you delete the reference to the iterator. In other words, after it is deleted, "iter++" has no idea where it needs to go next, because you just deleted "iter". You need to save where the iterator is in the list, so you just use the return value from erase(). All you would have to do to fix that code segment is change "names.erase(iter)" to "iter = names.erase(iter)" so you don't lose your position when you are traversing the list.
__________________


YOUR THROBBING MULTIFARIOUS LUSTFUL DESIRES ARE COMPLETED N YOUR HYPER-ORANGE SELF, YOU MAKE ME LOVE AGAIN, YOU'VE CHANGED MY HEART, MY MELANCHOLIA DISAPPEARS WHEN YOU ARE INSIDE OF ME, MY HUMAN RAGE IS TEMPERED WHEN I AM INSIDE YOU, THE SECRET IS COMMUNICATION, LONGEVITY, STAMINA, REPETITION, FURY, SOULFUL KISSING, EARPLUGS. YOU FUCKING CORPORATE COCKS AND CUNTS.

MY ANXIETY COMPLETE, MY DESIRE REPLETE, THE TASTE OF ORANGE BLOOD AND CUM AND GREENBACKS RUNNING DOWN MY FACE. THE STREETS WILL RUN ORANGE WITH YOUR MIXTURE OF CHEETOS AND HUNDRED DOLLAR BILLS REGURGITATED AND EATEN AND SHIT OUT AGAIN AND EATEN AGAIN.

YOU ARE MY SCULPTURE, MY SCULPTRA, MY SELF-DEFINITION. MY DEFINITION OF HUMANITY, MY HARMONY. MY HEART AND MY MIND.

YOU ARE SO ORANGE. SO CRUNCHY. SO CONSUMABLE.

THE NEW ORANGE UNDERGROUND IS THE ORANGE UP MY ASS. AND YOUR ASS.

I LOVE YOU CHEETOS.
FissionMailed1 is offline   Reply With Quote
Old 03-4-2013, 08:14 PM   #3
d4u7211
Aficionado of Awk
FFR Veteran
 
d4u7211's Avatar
 
Join Date: Oct 2006
Location: Wherever my computer is
Age: 30
Posts: 1,276
Send a message via Skype™ to d4u7211
Default Re: C++ iterator question

Quote:
Originally Posted by FissionMailed1 View Post
When you erase from std::list, you delete the reference to the iterator. In other words, after it is deleted, "iter++" has no idea where it needs to go next, because you just deleted "iter". You need to save where the iterator is in the list, so you just use the return value from erase(). All you would have to do to fix that code segment is change "names.erase(iter)" to "iter = names.erase(iter)" so you don't lose your position when you are traversing the list.
Ah, thank you very much :) i didn't really understand how .erase() worked (my book does not go into detail on it it seems...) - found http://www.cplusplus.com/reference/list/list/erase/ after your suggestion to further understand how it worked. I didn't know list::erase had a return value. (nor did i know if sending in the pointer to erase() would automatically dereference the variable or not, etc., if that was how it worked)
__________________
Hardpain of Hell RELEASED!!!

d4u7211 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 09:14 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution