Re: Storing a tree in a std::list<>
From: Dave (better_cs_now_at_yahoo.com)
Date: 06/06/04
- Next message: Steven T. Hatton: "Re: Why code completion and early error checking are needed"
- Previous message: Steven T. Hatton: "Re: [LONG] Re: Why code completion and early error checking are needed"
- In reply to: Jeff Schwab: "Re: Storing a tree in a std::list<>"
- Next in thread: David Harmon: "Re: Storing a tree in a std::list<>"
- Reply: David Harmon: "Re: Storing a tree in a std::list<>"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 5 Jun 2004 17:13:05 -0700
"Jeff Schwab" <jeffrey.schwab@comcast.net> wrote in message
news:YImdnVNNxPxZwV_dRVn-ug@comcast.com...
> Dave wrote:
> > Hello all,
> >
> > After perusing the Standard, I believe it is true to say that once you
> > insert an element into a std::list<>, its location in memory never
changes.
> > This makes a std::list<> ideal for storing vertices of an arbitrary
n-ary
> > tree where a vertex contain pointers to its parent / children. These
parent
> > / child vertices need to stay put if we've got pointers to them
somewhere!
> >
> > Am I correct in my assertion?
>
> I believe so. However, the idea doesn't seem especially graceful; what
> are its advantages to storing pointers to nodes in the list, rather than
> actual nodes? This achieves the same affect.
>
> As a side-note, the list could be used to store arbitrary graphs, not
> just trees. If you're only dealing with trees, what need is there for
> the list? You can get to any node from any other node by traversing the
> tree, you could order the nodes in a std::vector, etc.
My goal in using std::list<> is to avoid explicit memory management (and its
problems) but still have the convenient use of pointers. A node goes in the
list, and that node has pointers to its parent and children (which also
reside in the list). By having the nodes in a std::list, I can destroy an
arbitray tree in one fell swoop by simply destroying the list. No complex
memory management problems. Let the STL do it for me!
So, the list really isn't used as such. I don't iterate over it, sort it,
splice it or anything like that. It's just a way to have the whole tree
cleaned up automatically.
And as far as I can tell, once I put a node in the list, it will stay put -
its address will never change. list was chosen because this is not
necessarily true of other containers.
- Next message: Steven T. Hatton: "Re: Why code completion and early error checking are needed"
- Previous message: Steven T. Hatton: "Re: [LONG] Re: Why code completion and early error checking are needed"
- In reply to: Jeff Schwab: "Re: Storing a tree in a std::list<>"
- Next in thread: David Harmon: "Re: Storing a tree in a std::list<>"
- Reply: David Harmon: "Re: Storing a tree in a std::list<>"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|