erasing from stl list

Discussion in 'C++' started by Krzysztof Poc, Jan 18, 2010.

  1. Hi

    Is the following safe ?

    list<sth>::iterator it = myList . begin();
    while ( *it != myList.end() )
    myList . erase ( it ++ );

    great thanks for help
     
    Krzysztof Poc, Jan 18, 2010
    #1
    1. Advertising

  2. On Jan 18, 12:59 pm, Krzysztof Poc <> wrote:
    > Hi
    >
    > Is the following safe ?
    >
    > list<sth>::iterator it =  myList . begin();
    > while ( *it != myList.end() )
    >    myList . erase ( it ++ );
    >
    > great thanks for help


    Hi Krzysztof

    There is a typo in your code:
    while ( it != myList.end() )
    I have no reason your code isn't safe, but if you want
    to erase a list, there are two better ways:
    MyList.erase(MyList.begin(), MyList.end());
    or
    MyList.clear();
    Under Visual Studio 2008, the performance of erasing a range of
    iterators
    or to clear a list is much better. try the following code:

    #include <list>
    #include <iostream>

    int main()
    {
    using namespace std;

    list<int> L;
    for (int i = 0; i < 1000 * 1000; ++i)
    L.push_back(i);

    list<int>::iterator it = L.begin();
    L.erase(L.begin(), L.end()); // 1
    while (*it != L.end()) // 2
    L.erase(it++);
    L.clear(); // 3
    cout << "Done ... " << L.size() << '\n';
    return 0;
    }

    Regards,
    -- Saeed Amrollahi
     
    Saeed Amrollahi, Jan 18, 2010
    #2
    1. Advertising

  3. Krzysztof Poc

    Balog Pal Guest

    "Krzysztof Poc" <>
    > Is the following safe ?
    >
    > list<sth>::iterator it = myList . begin();
    > while ( *it != myList.end() )
    > myList . erase ( it ++ );


    Yes. What problem you suspect?
     
    Balog Pal, Jan 18, 2010
    #3
  4. Krzysztof Poc

    Bas Guest

    "Krzysztof Poc" <> wrote in message
    news:...
    > Hi
    >
    > Is the following safe ?
    >
    > list<sth>::iterator it = myList . begin();
    > while ( *it != myList.end() )
    > myList . erase ( it ++ );
    >
    > great thanks for help


    its safe.
    You cannot do this with vector, though.

    bas from holland
     
    Bas, Jan 19, 2010
    #4
  5. Bas wrote:
    >
    > "Krzysztof Poc" <> wrote in message
    > news:...
    >> Hi
    >>
    >> Is the following safe ?
    >>
    >> list<sth>::iterator it = myList . begin();
    >> while ( *it != myList.end() )
    >> myList . erase ( it ++ );
    >>
    >> great thanks for help

    >
    > its safe.
    > You cannot do this with vector, though.
    >

    vector<sth>::iterator it = myVector . begin();
    while ( it != myVector.end() )
    it = myVector . erase (it);

    though it's not that efficient ;)

    Greets
     
    Branimir Maksimovic, Jan 19, 2010
    #5
  6. In message <hj3tod$opt$>, Branimir Maksimovic
    <> writes
    >Bas wrote:
    >> "Krzysztof Poc" <> wrote in message
    >>news:...
    >>> Hi
    >>>
    >>> Is the following safe ?
    >>>
    >>> list<sth>::iterator it = myList . begin();
    >>> while ( *it != myList.end() )
    >>> myList . erase ( it ++ );
    >>>
    >>> great thanks for help

    >> its safe.
    >> You cannot do this with vector, though.
    >>

    > vector<sth>::iterator it = myVector . begin();
    > while ( it != myVector.end() )
    > it = myVector . erase (it);
    >
    >though it's not that efficient ;)
    >

    Last time I looked, the member function clear() was both safe and
    efficient for erasing all the contents of all std:: containers. Of
    course, if the original question wasn't really about erasing _all_
    members, it's a different matter ;-)

    --
    Richard Herring
     
    Richard Herring, Jan 19, 2010
    #6
  7. On Jan 18, 4:38 pm, "Balog Pal" <> wrote:
    > "Krzysztof Poc" <>
    >
    > > Is the following safe ?

    >
    > > list<sth>::iterator it =  myList . begin();
    > > while ( *it != myList.end() )
    > >   myList . erase ( it ++ );

    >
    > Yes. What problem you suspect?


    Thanks for an answer.
    My comments in the code below:

    list<sth>::iterator it = myList . begin();
    while ( it != myList.end() )
    myList . erase ( it ++ ); // I expect this line
    // to be unsafe e.g. iterator
    // is invalidated before it is removed.
     
    Krzysztof Poc, Jan 21, 2010
    #7
  8. Krzysztof Poc

    Balog Pal Guest

    "Krzysztof Poc" <>

    >list<sth>::iterator it = myList . begin();
    >while ( it != myList.end() )
    > myList . erase ( it ++ ); // I expect this line
    > // to be unsafe e.g. iterator
    > // is invalidated before it is removed.


    The () of function call impose sequence points. So that line works as:

    {
    iterator curr = it;
    it = it + 1;
    erase(curr);
    }
     
    Balog Pal, Jan 21, 2010
    #8
  9. Krzysztof Poc

    Jerry Coffin Guest

    In article <e2cbd3f6-886c-44e8-b06a-
    >, says...

    [ ... ]

    > list<sth>::iterator it = myList . begin();
    > while ( it != myList.end() )
    > myList . erase ( it ++ ); // I expect this line
    > // to be unsafe e.g. iterator
    > // is invalidated before it is removed.


    Why would you do this at all, given that once you're sure you've made
    it work correctly, it's still just a less readable version of
    'myList.clear();' ?

    --
    Later,
    Jerry.
     
    Jerry Coffin, Jan 21, 2010
    #9
  10. Krzysztof Poc

    Balog Pal Guest

    "Jerry Coffin" <>
    >> list<sth>::iterator it = myList . begin();
    >> while ( it != myList.end() )
    >> myList . erase ( it ++ ); // I expect this line
    >> // to be unsafe e.g. iterator
    >> // is invalidated before it is removed.

    >
    > Why would you do this at all, given that once you're sure you've made
    > it work correctly, it's still just a less readable version of
    > 'myList.clear();' ?


    guess simplified for the purpose -- in normal life there is many such loops
    that have that erase inside a conditionsal block.
     
    Balog Pal, Jan 22, 2010
    #10
  11. On Jan 21, 8:53 pm, Jerry Coffin <> wrote:
    > In article <e2cbd3f6-886c-44e8-b06a-
    > >, says...
    >
    > [ ... ]
    >
    > > list<sth>::iterator it =  myList . begin();
    > > while ( it != myList.end() )
    > >    myList . erase ( it ++ ); // I expect this line
    > >      // to be unsafe e.g. iterator
    > >      // is invalidated before it is removed.

    >
    > Why would you do this at all, given that once you're sure you've made
    > it work correctly, it's still just a less readable version of
    > 'myList.clear();' ?
    >
    > --
    >     Later,
    >     Jerry.


    Assume the following code:

    list<sth*>::iterator it = myList . begin();
    while ( it != myList.end() )
    {
    (*it)->ReleaseResources();
    myList.erase(it++);
    }

    If the list is long it is scanned only once. I guess that with clear
    the list
    would be scanned twice in the above example.
     
    Krzysztof Poc, Jan 22, 2010
    #11
    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. Generic Usenet Account
    Replies:
    8
    Views:
    406
    Generic Usenet Account
    Jan 3, 2004
  2. Gernot Frisch

    erasing from a list within a loop?

    Gernot Frisch, Jul 29, 2004, in forum: C++
    Replies:
    12
    Views:
    542
    Gernot Frisch
    Aug 3, 2004
  3. Tescobar
    Replies:
    12
    Views:
    500
    Vyacheslav Kononenko
    Jul 25, 2005
  4. A B
    Replies:
    5
    Views:
    502
    Juha Nieminen
    Nov 18, 2010
  5. A B
    Replies:
    5
    Views:
    614
Loading...

Share This Page