Results 1 to 6 of 6

Thread: List: Erase element at iterator during iterations

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    List: Erase element at iterator during iterations

    I em having trouble with a list iteration.

    I have to tranverse a list rather frequently, if an element is flagged for deletion then it must be removed.

    for (list<int> iter = l.begin(); iter != l.end(); ++iter)
    {
    iter = l.erase(iter);
    cout << (*iter) << endl;
    }

    This deletes every even number... since the first element is deleted, the 2nd is returns, then it is incremented by the for loop and deleted.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  2. #2
    Lively Member
    Join Date
    Nov 2005
    Posts
    68

    Re: List: Erase element at iterator during iterations

    Increment "manually"
    for (iter = l.begin(); iter!=l.end()
    {
    if (must erase)
    iter = l.erase(iter);
    else
    ++iter;
    }
    "bla, bla,... exists number M so for each n > M bla, bla..." Exists? Where is it? (Kronecker said...)

  3. #3
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    Re: List: Erase element at iterator during iterations

    go through the list backwards

    for (list<int> iter = l.end(); iter != l.begin(); --iter)
    {
    l.erase(iter);
    cout << (*iter) << endl;
    }

  4. #4
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: List: Erase element at iterator during iterations

    This will not work, when you call l.erase(iter) the iterator becomes invalid, any other action with that iterator is illegal.

    The idiomatic C++ way to do what you want to do is using list::remove_if. An alternative is to increment the iterator before erasing it:
    Code:
    for (list<int>::iterator iter = l.begin(); iter != l.end(); )
    {
        list<int>::iterator to_erase = iter;
        ++iter;
        if (i_want_to_erase_it) {
            iter = l.erase(to_erase);
        }
    }

  5. #5

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: List: Erase element at iterator during iterations

    twanvl && bilm_ks methods worked fine with the expected results.

    Now, twanvl your method works identically to bilm_ks, I cannot see why your method is more "correct".

    If you go iter = l.erase(iter). The returned iterator is valid.
    If you do not erase then ++iter.

    So

    while (iter != l.end())
    {
    if (erase) iter = l.erase(iter);
    else ++iter;
    }
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: List: Erase element at iterator during iterations

    Indeed, bilm_ks's method is also recommended by Scott Meyers in Effective STL.

    But twanvl is right, remove_if would be the typical way to do this.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width