Thread: erasing the i'th element from std::list

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    erasing the i'th element from std::list

    Hi.

    given a list and an index i (of type int), how can I erase the i'th element using the STL library?

    to be more specific:

    I have a class named RadioStation, that holds a list of Playlists as an instance variable:
    Code:
    list<Playlist> m_playlists;
    and I need to implement the following method:
    Code:
    bool removePlayList(unsigned iPlaylistNumber);
    the above method supposed to erase the Playlist whose index is iPlayListNumber.

    the naive way to do it (or at least the one that I came up with) is this:
    since list::erase takes an iterator to a position, I first set an iterator to the very begining of the list:
    Code:
    list<Playlist>::iterator iter=m_playlists.begin();
    then advancing iter iPlaylistNumber steps ahead:
    Code:
    std::advance(iter,iPlaylistNumber);
    and finally erasing the element:
    Code:
    m_playlists.erase(iter);
    is there other, easier way to do it?

    thanks in advanced!

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485

    m_playlists.erase(m_playlists.begin() + i);


    make sure the element exists beforehand


    Oops. I didn't read carefully and was thinking of vectors for some reason.
    Last edited by SirPrattlepod; 10-02-2013 at 06:21 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you know the position (or index) of the element you wish to remove, and have your heart set on using std::list, that's probably the easiest way. list's iterators do not support adding integral values (other than 1) in order to advance.

    Given that you are referring to elements of a list using an integral index, you might be better off using a vector. Vector's iterators provide alternative methods of obtaining an iterator corresponding to an index.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by SirPrattlepod View Post


    Oops. I didn't read carefully and was thinking of vectors for some reason.
    Yeah? Well, welcome to my ignore list fool!

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    228
    Thanks grumpy.
    I relize vector gives random access, wich will make it more easier here, but list is more suitable for my purposes in this case.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Absurd View Post
    list is more suitable for my purposes in this case.
    with modern C++ compilers and hardware, the advantage that list has over vector is rapidly shrinking.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    228
    Quote Originally Posted by Elkvis View Post
    with modern C++ compilers and hardware, the advantage that list has over vector is rapidly shrinking.
    I agree, but since this is part of a class assignment, the grade is given by our choices on which containers to use in what case.
    the more efficient the code, the less points we'll lose.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    out of curiosity, what makes std::list more suitable for your needs? it's less efficient on storage, less efficient when accessing elements by index, and can only be more efficient when adding and removing items, and even then, it's not a guarantee.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    May 2013
    Posts
    228
    well, mainly because beside removePlaylist(int iPlaylistNumber);, I'm also implementing (and using quite a lot) removePlaylist(Playlist pl); where std::list removes objects in O(1), instead of O(n) with std::vector.

  10. #10
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elkvis View Post
    with modern C++ compilers and hardware, the advantage that list has over vector is rapidly shrinking.
    WAT ?!!

    Elaborate...

    Quote Originally Posted by Elkvis View Post
    and can only be more efficient when adding and removing items, and even then, it's not a guarantee
    It is guaranteed.

    @OP:

    How I would recommend to do it not for an assignment:


    1. Make abstraction of the list; encapsulate either std::list or std::vector (whatever is easier to use).
    2. Ask yourself if the performance is actually a problem. If it is, then optimise (possibly switching to a different container).

    How I would recommend to do it for an assignment:

    The problem is that it is harder to optimise for both, insertion/erasure and random access, at the same time. You can certainly get a mixture of the two. One solution is to use std::list and cache (that is, remember) any random access.

    How to implement random access caching?

    First, you have to make an abstraction. You should create a class "Playlists" which would internally keep:
    - an std::list holding your playlists;
    - iterator to the recently accessed element;
    - index of the recently accessed element.
    Whenever you randomly access element of your container, you can make use of the iterator and its associated index to quickly move to the desired element of std::list. It means that if the recently visited element was at index 100, and now you want to visit element at index 101, the class "Playlists" will (under the hood) increment the internal iterator and its associated index by one, and then return dereferenced value. If the next element is at index 102, then again, it will increment iterator and its associated index only by one.

    The proposed random access caching is only one of the solutions.

    But do you really need a list? What about a map:
    Code:
    std::unordered_map<std::string, Playlist> playlists;
    Random access, insertion, and erasure would all have constant time in average (std::map would guarantee logarithmic). Additionally, you can use mnemonical names instead of ugly indices.
    Last edited by kmdv; 10-03-2013 at 01:16 PM.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elkvis View Post
    with modern C++ compilers and hardware, the advantage that list has over vector is rapidly shrinking.
    I disagree.

    list and vector are specified to have different complexity of some operations (appending to the beginning or end, inserting in the middle, counting the elements, etc). Depending on the pattern of use of the container and its size, there are distinct advantages of each container type over another.

    Modern compilers and hardware have an effect, sure. But they can't generally turn high complexity operations into low complexity operations. And they are limited by the dependencies between operations.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by kmdv View Post
    Elaborate...
    if the entire vector can be stored in the processor's cache memory, it's possible for vector to be faster than list for all operations, because of the nodal nature of a list. it's conceivable that in a worst case, every time you retrieve the next element of a linked list, it could cause a cache miss and force the processor to fetch it from main memory.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Bjarne Stroustrup made exactly this point in GoingNative2012 (short video of that segment)
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-23-2013, 06:11 AM
  2. How to get last element of list?
    By dnguyen1022 in forum C++ Programming
    Replies: 9
    Last Post: 04-04-2009, 11:48 AM
  3. Remove element from STL list
    By Micko in forum C++ Programming
    Replies: 7
    Last Post: 07-05-2007, 11:07 AM
  4. list first element
    By wyrwidab in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2005, 11:42 AM
  5. sorting a vector after erasing element?
    By aker_y3k in forum C++ Programming
    Replies: 6
    Last Post: 05-01-2003, 07:44 PM