list iterator

Discussion in 'C++' started by Flwz, May 23, 2005.

  1. Flwz

    Flwz Guest

    There's something I don't quite understand, sorry if that sound stupid
    or whatever ; )

    why is :

    for( it = Flare::flareList.begin() ; it != Flare::flareList.end(); it++)
    if (it->isWorn())
    {
    it = Flare::flareList.erase( it);
    // Flare::flareList.push_back( Flare());
    }

    not working ? Since erase is supposed to return a valid iterator or the
    end of the list... I made a workaround but it doens't look so cool to me


    bool through;
    do
    {
    through = true;

    for( it = Flare::flareList.begin() ; it != Flare::flareList.end(); it++)
    if (it->isWorn())
    {
    it = Flare::flareList.erase( it);
    // Flare::flareList.push_back( Flare());
    through = false;
    break;
    }
    }
    while (!through);


    That works but it's neither efficient nor pretty, so if anyone could
    explain me what's going on in the first case and a better work around ;)
    thanks !
    Flwz, May 23, 2005
    #1
    1. Advertising

  2. Flwz

    Flwz Guest

    Flwz a écrit :
    > There's something I don't quite understand, sorry if that sound stupid
    > or whatever ; )
    >
    > why is :
    >
    > for( it = Flare::flareList.begin() ; it !=
    > Flare::flareList.end(); it++)
    > if (it->isWorn())
    > {
    > it = Flare::flareList.erase( it);
    > // Flare::flareList.push_back( Flare());
    > }
    >
    > not working ? Since erase is supposed to return a valid iterator or the
    > end of the list... I made a workaround but it doens't look so cool to me
    >


    Ok alright, I found out (why is it always just after I post ? ;)
    if it is at the end of the list after the erase, it still does it++
    before checking the end for statement, so it obviously will crash ; )
    Flwz, May 23, 2005
    #2
    1. Advertising

  3. Flwz wrote:
    > There's something I don't quite understand, sorry if that sound stupid
    > or whatever ; )
    >
    > why is :
    >
    > for( it = Flare::flareList.begin() ; it !=
    > Flare::flareList.end(); it++)
    > if (it->isWorn())
    > {
    > it = Flare::flareList.erase( it);


    You assign the value here and also do it++ after every iteration. You
    should only do either of them.

    > // Flare::flareList.push_back( Flare());
    > }
    >
    > not working ? Since erase is supposed to return a valid iterator or the
    > end of the list... I made a workaround but it doens't look so cool to me
    >
    >
    > bool through;
    > do
    > {
    > through = true;
    >
    > for( it = Flare::flareList.begin() ; it !=
    > Flare::flareList.end(); it++)
    > if (it->isWorn())
    > {
    > it = Flare::flareList.erase( it);
    > // Flare::flareList.push_back( Flare());
    > through = false;
    > break;
    > }
    > }
    > while (!through);
    >
    >
    > That works but it's neither efficient nor pretty, so if anyone could
    > explain me what's going on in the first case and a better work around ;)
    > thanks !


    for (it = Flare::flareList.begin(); it != Flare::fllareList.end(); )
    {
    if (it->isWorn())
    it = Flare::flareList.erase(it);
    else
    ++it;
    }

    should do it.

    V
    Victor Bazarov, May 24, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,418
  2. greg
    Replies:
    6
    Views:
    454
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    642
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,156
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,039
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page