Hi guys,
I have list of class contain list of another class.

MyMessages class
{
ID
list <myPackets> Packets;
}

myPackets class
{
Seq
Text
}

I need advice which one is the right way to set myMessages class??

void set_myMessages(const int &ID, list <myPackets> Packets)

OR

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,

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

Dani AI

Generated

Quick practical guidance (builds on what and touched on). Prefer passing containers by const reference (or by move in modern C++) instead of copying them into a setter. Also prefer a singular, clear name for the element type (e.g., Packet) and use std::vector unless you specifically need std::list semantics (splicing, stable iterator references). Example setter signatures:

void setMessages(int id, const std::vector<Packet>& packets);
void setMessages(int id, std::vector<Packet>&& packets); // move-friendly (C++11+)

To print the contents, give each element a printable form (either overload operator<< or provide a toString()), then iterate. A short example:

std::ostream& operator<<(std::ostream& os, const Packet& p) {
    return os << p.seq << ": " << p.text;
}

for (const Packet& p : packets) std::cout << p << '\n';

For sorting, prefer std::sort on vector (fast, random access) or list::sort if you stay with list. Use a comparator or implement operator<:

std::sort(vec.begin(), vec.end(),
          [](const Packet& a, const Packet& b){ return a.seq < b.seq; });

packets.sort([](const Packet& a, const Packet& b){ return a.seq < b.seq; }); // for std::list

Notes and gotchas: copying large containers is expensive because nodes/elements are allocated on the heap; it’s not solved by changing linker stack settings — avoid unnecessary copies by using const references or move semantics. If equal seq values must preserve insertion order, use std::stable_sort (vector) or ensure your comparator accounts for a secondary key. Finally, keep member names and responsibilities small and explicit — that clears up printing/sorting code and makes debugging easier (congrats to for getting it working).

Recommended Answers

All 3 Replies

You can look all that info up in a STL List reference, as the one found here:

See the "STL Algorithms" for sorting data inside STL containers.

void set_myMessages(const int &ID, list <myPackets> Packets)

Hmm , good question , I have the past experience with dealing these
type of question. where you have to consider is list <myPacket> will
have a default constructor and a copy constructor and a assignment operator. Then this will work.

But if you using a big datastructures like this tell the linker to generate a big stack.Otherwise stackoverflows will be there.

guys thank you for all the input,
Finally I be able to make it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.