Thread: STL List Erase

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    STL List Erase

    Hmm. I do not generally use the STL alot, but I have a large code base that uses STL list. I was growing fond of it until....erase. Is there a way to erase an element from the list without having the list structure specifically. All I want is to be able to write:

    Code:
    std::list<int> foo;
    foo.push_back(5);
    foo.push_back(6);
    
    std::list<int>::iterator bar;
    bar = foo.begin();
    bar++;
    then

    Code:
    bar.erase();
    or

    Code:
    erase(bar);
    or some such. I have looked through the stl code and I can't find the function so its either not there or I am looking in the wrong place. Thanks for the help

  2. #2

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think you'd need to create a list that returns iterators that also contain a reference back to the list that they came from.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    I think you'd need to create a list that returns iterators that also contain a reference back to the list that they came from.
    You don't have to rewrite std::list, just make a new kind of iterator (based on the standard iterator).

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    You don't have to rewrite std::list, just make a new kind of iterator (based on the standard iterator).
    But how would you get std::list to return your custom iterator instead of its own?

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    Custom Iterator

    I am guessing as some have mentioned I would need a custom iterator. Obviously it can't be a member function of list (unless it is a static member, which I don't think there is). It needs to be a member function of the list::iterator class. If you look at the list::erase function it is very simple I suppose I could just copy the same thing into the iterator class.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I'm afraid you shouldn't modify either std::list not std::list::iterator.

    You might be able to create your own free iterator that takes a reference to a std::list in its constructor so that it can provide the desired syntax for erase, but probably look ugly for anything else...

    Why do you have a problem with list.erase(iterator) in the first place?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Sorry, I misread the question the first time.

    I would think that remembering which list the iterator belonged to should be state added to whatever code was handling that iterator.

    Creating a custom iterator or list seems like it would only make sense if you had (or expected to have) this problem in several unrelated areas of your application.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM