Thread: a List of Lists on C++

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    13

    a List of Lists on C++

    Hi =D, i want to make a list of lists that contain structures, like this:

    Code:
    typedef struct{
    
    	char        AAAA[20+1];
    	char	       BBBB[40+1];
            char        CCCC[60+1];
    
    } Letters;
    
    typedef std::list<Letters> listLetters;
    typedef std::list<Letters>::iterator  IteratorListLetters;
    Now i want a list of "listLetters", i tried this:

    Code:
    std::list<listLetters> ListofListLetters;
    but it results on some warnings...

    Anyone can tell me the right way to do this? i really have to make a list of lists of struct.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No idea - it compiles for me.

    You're going to have to post some actual code and some actual error messages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    what compiler are u using? i'm using the compiler of MS Visual Studio 6.

    the code is the same of the post, and this is the warnings:

    c:\arquivos de programas\microsoft visual studio\vc98\include\list(125) : warning C4786: '?$reverse_bidirectional_iterator@Viterator@?$list @V?$list@UstInfo@@V?$allocator@UstInfo@@@std@@@std @@V?$allocator@V?$list@UstInfo@@V?$allocator@
    UstInfo@@@std@@@std@@@2@@std@@V?$list@UstInfo@@V?$ allocator@UstInfo@@@std@@@3@AAV43@PAV43@H' : identifier was truncated to '255' characters in the browser information
    C:\PROJECTS\FontesVc\TestSrv\Srv.cpp(75) : see reference to class template instantiation 'std::list<class std::list<struct stInfo,class std::allocator<struct stInfoTag> >,class std::allocator<class std::list<st
    Last edited by Rafa T.; 01-02-2011 at 08:29 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    g++ 4.4.3

    Interesting that you mention Visual Studio 6.
    It's well over 10 years old now, and is known to be quite buggy in the STL.
    Example of how to get past the problem
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    i know man, i use g++ too, but its work....

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    13

    another doubt...

    how i access a member of the list inside of the list?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Rafa T.
    i know man, i use g++ too, but its work....
    If you are maintaining an existing program then too bad, I suppose. Otherwise, I suggest that you fight for an upgrade to a newer compiler that conforms better to the C++ standard.

    As for warning: you might be able to just ignore it since it is about a potential problem with name mangling producing too long a name.

    Quote Originally Posted by Rafa T.
    how i access a member of the list inside of the list?
    The usual ways, e.g., by an iterator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    But how i use the interator of the list inside of the list? how i acess members of the list inside of the list?

    is a list of lists of structs, i need to compare a field of the struct, like:

    ListOfLists->ListX->field01 == "AAA";

    i can acess ListOfLists with interator, but what i have to do to acess ListX inside of ListOfLists, with ListofLists iterator? Can u give me an example?


    TY for information.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if you have
    Code:
    typedef list<Letter> listLetters;
    typedef list<listLetters> listlistLetters;
    then an iterator through a listlistLetters will in turn point at all the listLetters inside the bigger list.
    Code:
    listlistLetters bob;
    listlistLetters::iterator ptr = bob.begin();
    //ptr now points at the first list-of-letters inside the big list
    ptr++;
    //ptr now points at the second list-of-letters inside the big list
    //etc.

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    almost this... i need to acess the field of one of the ListLetters that are inside ListListLetters

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Rafa T. View Post
    almost this... i need to acess the field of one of the ListLetters that are inside ListListLetters
    Then do so. Since it's a list, you don't have much choice but to start walking at ptr->begin() and advance forward until you get to ptr->end().

  12. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    An example please guys.... how i use the iterators of the ListLetterx, that is inside od ListListLetter...

    ListListLetter
    |
    |------- ListLetterx|
    | |---- AAAA
    | |
    | |---- BBBB
    |
    |------- ListLettery|
    |---- CCCC
    |
    |---- DDDD


    i need to acess "AAAA" or "BBBB", the value inside the list that is inside the list. Help me =/

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Here is an example:
    Code:
    #include <iostream>
    #include <list>
    
    struct Letters {
        char AAAA[20 + 1];
        char BBBB[40 + 1];
        char CCCC[60 + 1];
    };
    
    typedef std::list<Letters> LettersList;
    typedef std::list<LettersList> LettersListList;
    
    int main()
    {
        using namespace std;
        LettersListList list;
        // Populate list
        // ...
    
        // Print list
        for (LettersListList::iterator i = list.begin(), i_end = list.end(); i != i_end; ++i)
        {
            for (LettersList::iterator j = i->begin(), j_end = i->end(); j != j_end; ++j)
            {
                cout << "AAAA: " << j->AAAA
                    << "BBBB: " << j->BBBB
                    << "CCCC: " << j->CCCC << endl;
            }
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jul 2010
    Posts
    13

    Thank You

    Thank you and all here for the help, it worked!

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't forget that you need to use strcmp to compare the strings since they're pure C-strings!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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