Thread: List of lists

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    11

    List of lists

    How would I access a list inside a list? Say for...

    Code:
    list<int> inside;
    list<list<int>* > outside;
    list<list<int> *>::iterator outIter;
    list<int>:: iterator inIter;
    outside.push_front(&inside);
    outIter = outside.begin();
    
    inIter =   ???;

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Is there a reason your inner list is a pointer? Why not just a list of lists?

    As is, you'd need to dereference the outIter iterator, which would give you a list pointer. Then you'd ned to dereference the list pointer and call the member function begin() to get an iterator pointing at the first int. So:
    Code:
    inIter = (*outIter)->begin();
    Of course, in the above code, you would never want to use inIter, because the inside list is empty.

    If you got rid of the pointer, it would look like this:
    Code:
    list<int> inside;
    list<list<int> > outside;
    list<list<int> >::iterator outIter;
    list<int>::iterator inIter;
    outside.push_front(inside); // note that a copy is pushed on to the list
    outIter = outside.begin();
    
    inIter = outIter->begin();

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    Thanks, was using pointer just becuase thats the way I had a vector of lists set up before. Just wanted to pick up the concept of how to work a list of list and wasn't sure how.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  2. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  3. link list
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2001, 05:41 AM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM