help with STL lists

This is a discussion on help with STL lists within the C++ Programming forums, part of the General Programming Boards category; Have decided to implement the linked lists that come as a part of the standard template library in a program ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    110

    help with STL lists

    Have decided to implement the linked lists that come as a part of the standard template library in a program that I am writing, though am a bit confused as to the functions that they use and such.

    Anyway, I am using the Dietel and Dietel C++ How to Program book as a starting point, though it only covers them in verry limited detail.

    What I need to do is once I have information stored in the list ( this part is working properly due to the stl implementing lists through templates ) is to then be able to access the information both sequentialy ( through use of an iterator ) and non-sequentialy ( buy retrieving the stored data by its element number ).

    This I have no idea how to do.

    Any help with this would be greately apreciated.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    list's don't support random access, ie "by element number" you have to do something like this
    Code:
    template<class T>
    std::list<T>::iterator get_n(std::list<T> &l, int n) {
        std::list<T>::iterator it = l.begin();
    	while(n>0 && it != l.end()) {it++;n--;}
    	return it;
    }
    This is painfully slow, in general the main reason you want a list is for "destructive splice" If you want to remove a sequence of elemetns from one container and insert it into the middle of another then list is the way to go. std::vector is probably what you really want.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Thanks for your help.

    Turns out vectors are what I am after.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intersection of two STL lists
    By misterMatt in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2009, 12:25 AM
  2. STL Linked Lists
    By stanlvw in forum C++ Programming
    Replies: 12
    Last Post: 04-11-2008, 01:33 AM
  3. Question about using erase() with lists (STL)
    By apacz in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2005, 10:51 AM
  4. A question about STL Lists.
    By joenching in forum C++ Programming
    Replies: 9
    Last Post: 04-25-2005, 09:23 PM
  5. Stl lists and user defined types
    By figa in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2005, 11:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21