DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 8 of 8

Thread: STL lists

  1. #1
    Join Date
    May 2006
    Posts
    48

    STL lists

    I'm wondering if I can create a list inside a list inside STL and how I would do that? So it is sort of a 2d linked list.

    Lets say I have two classes, bookTypes and books... I want to create a list of bookTypes like history, mystery, etc. And then add the list containing names of books belonging to each type.

    I have this for the startup:
    std::list<bookTypes> types;
    typedef std::list<bookTypes>::iterator typeIt;

    bookTypes myNewBookType;
    myNewBookType.add(typeOfBook); //some add function from the bookTypes class
    types.pushback(myNewBookType);

    for (typeIt=types.begin(); typeIt!=types.end(); ++typeIt)
    {

    // NOW HOW DO I ADD THE NAME OF THE BOOK IN HERE FOR EACH BOOK TYPE
    //thinking of doing stuff to *typeIt but not sure how

    }

    also just for confirmation, this adds new items to the front of the list, right? So when I do pushback all the prior items will be pushed back and last added item will be popped first right?

    Thanks!

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Of course you can use a list as the element type of another list:
    Code:
    typedef list<int> IntList;
    typedef IntList::iterator IntListIter;
    typedef list<IntList> ListList;
    typedef ListList::iterator ListListIter;
    
    /// ...
    ListList ll;
    // resize it/initialise it....
    
    for(ListListIter listIt = ll.begin(); listIt != ll.end(); listIt++)
    {
         for(IntListIter intIt = listIt->begin(); intIt != listIt->end(); intIt++)
         {
              *intIt = someIntReturningFunction();
         }
    }
    Cheers,

    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  3. #3
    Join Date
    May 2006
    Posts
    48
    well, in that *intIt = someIntReturningFunction(); is that function part of IntList or ListList? I'm trying to access lets say a feed function that is part of the nested list (ListList).

  4. #4
    Join Date
    May 2006
    Posts
    48
    Also i wanted to ask, let say I wanna do something at a particular location... can I do something like:

    for (intIt = listIt->begin(); intIt != listIt->end(); intIt++)
    {
    if(intIt == 3)
    {
    // do something here
    }
    }

  5. #5
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    someIntReturningFunction() is just a place holder because I didn't want to suggest you have to insert a particular int value.
    you could write
    Code:
    *intIt = 4711;
    if you like, which sets all values in the list to the same value 4711.
    If you want random access to the elements, you should consider using a different STL container (vector/map) for perfomance issues, but you can still address list elements by random access. Just be aware, that the list is iterated from the beginning.
    Code:
    list<int> iList;
    // fill the list
    for(int i= 0; i < 10000000; i++)
       iList.push_back(i*i);
    
    iList[898889] = 0; // uses the std::list + - operator 898889 times starting from iList.begin(): NIGHTMARE!!!
    think about which container you use for which purpose.

    vector is ideal when your index type is int and you never need to delete or insert elements somewhere in the middle and you need random access to the elements
    map is a good choice when you need to delete and insert frequently and still need random access to elements
    list is good when you only want to access elements in order (not random access) and insert/delete at your current position

    These are just guidelines, so think about the problem at hand and how you want to use your structures.

    Cheers,
    D

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    If you need random access to a container's elements (e.g., the third element), you should probably use vector, not list
    Danny Kalev

  7. #7
    Join Date
    May 2007
    Posts
    843

    Thumbs up

    I probably understand what is going here.

    The problem is 2d List

    booktypes book book book
    C++ 1b 2b 3b
    C 1b 1b 1b

    Do you mean like this ?

    I wonder why u sing typedef because it is very confusing.

    I seldom used it since class also make the user defined type.

    Please correct me if i wrong.

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    No, I meant something like:

    X x;
    x[3]= 5;

    int z= x[4];
    etc.
    Danny Kalev

Similar Threads

  1. vectors vs. lists timings
    By ami in forum C++
    Replies: 4
    Last Post: 01-08-2007, 04:50 PM
  2. Replies: 0
    Last Post: 09-19-2005, 07:21 AM
  3. Replies: 0
    Last Post: 01-11-2002, 04:46 AM
  4. automating distribution lists
    By Jamie Harsevoort in forum Enterprise
    Replies: 0
    Last Post: 11-30-2000, 04:34 PM
  5. ANSI/ISO C++ compliance of VC++ 7.0 sucks! ...
    By Shiv Shankar Ramakrishnan in forum .NET
    Replies: 29
    Last Post: 09-01-2000, 03:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links