Thread: Need advice about list of lists

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Need advice about list of lists

    Hi guys,
    I have list of class contain list of another class.
    Code:
    MyMessages class
    {
       ID
       list <myPackets> Packets;
    }
    
    myPackets class
    {
       Seq
      Text
    }
    I need advice which one is the right way to set myMessages class??

    Code:
    void set_myMessages(const int &ID, list <myPackets> Packets)
    OR
    Code:
    void set_myMessages(const string &ID, string Seq, string Text)
    and also I need advice how to make a list of class.
    atm I use,

    Code:
    list <myPackets> listPackets;
    myPackets Packets;
    
    Packets.set(seq,text);
    listPackets.push_back(Packets);
    
    Packets.set(seq,text);
    listPackets.push_back(Packets);
    It does work but, I couldn't print the listPackets using iterator?
    so How do I print my list??? Also I need to sort list of my Packets, but
    my list of packet contain 2 element seq and text, so I couldn't use sort() funtion, so any Idea how I sorting the data??
    Thank You So Much

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by handytxg
    I need advice which one is the right way to set myMessages class??
    You might want to elaborate on what your MyMessages class models, what is the class interface that you currently have in mind, and what this function in question is supposed to do.

    Quote Originally Posted by handytxg
    It does work but, I couldn't print the listPackets using iterator?
    Using an iterator is probably the right way to do traverse a std::list.

    Quote Originally Posted by handytxg
    Also I need to sort list of my Packets, but
    my list of packet contain 2 element seq and text, so I couldn't use sort() funtion, so any Idea how I sorting the data??
    If it makes sense in general, you could overload operator< for your myPackets class. If it does not make sense in general, then you could define a predicate function or function object that compares myPackets objects with a "less than" ordering, and then you can pass that function or function object to the std::list sort member function.
    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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    laserlight thank you for all the input,
    Finally I be able to make it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  2. Merging two lists as one
    By Lone in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2005, 03:59 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  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