C++ iterator question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • d4u7211
    Aficionado of Awk
    • Oct 2006
    • 1276

    #1

    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!!!

  • FissionMailed1
    FFR Player
    • Feb 2012
    • 1267

    #2
    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.

    Comment

    • d4u7211
      Aficionado of Awk
      • Oct 2006
      • 1276

      #3
      Re: C++ iterator question

      Originally posted by FissionMailed1
      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!!!

      Comment

      Working...