stl data structure to represent a tree

Discussion in 'C++' started by puzzlecracker, Sep 18, 2005.

  1. I am looking for a data structure or perhaps a container that I can use
    to represent linux-like file system (each node in the tree containes
    needed fields, i.e. permissions, d/f, etc: for this I am thing of
    mapping node to a structure.. any commetns about this).


    Any suggestions (other then implementing my own tree)?
    Thanks
    puzzlecracker, Sep 18, 2005
    #1
    1. Advertising

  2. puzzlecracker

    Daw Guest

    Maybe you can make a structure like

    struct FS {
    // permissions, d/f etc.
    };

    and then use some container like

    std::vector<FS> nodes;
    Daw, Sep 18, 2005
    #2
    1. Advertising

  3. I am thinking to have something like this:

    struct Entry{

    union {
    std::stringBuffer info; // dont remembe the exact equivalent for
    sting buffer :)
    std::vector<Entry>;
    }type;


    };
    what is the best way to distinguesh the current content of the union?

    thx
    puzzlecracker, Sep 18, 2005
    #3
  4. I forgot that unions have some issues with nontrivial ctors...

    so here my another attempt


    struct Entry{

    all flags shared by directory and files



    };


    struct FileEntry:Entry{

    string buffer

    };



    struct DirEntry:Entry{

    vector<Entry> ve;

    };

    I am thing along this lines..


    I am also considering forwarding approach... any pro/cons?
    puzzlecracker, Sep 18, 2005
    #4
  5. puzzlecracker wrote:
    > I forgot that unions have some issues with nontrivial ctors...
    >
    > so here my another attempt
    >
    >
    > struct Entry{
    >
    > all flags shared by directory and files
    >
    >
    >
    > };
    >
    >
    > struct FileEntry:Entry{
    >
    > string buffer
    >
    > };
    >
    >
    >
    > struct DirEntry:Entry{
    >
    > vector<Entry> ve;
    >
    > };
    >
    > I am thing along this lines..
    >
    >
    > I am also considering forwarding approach... any pro/cons?
    >


    Your code above suffers from slicing, you need pointers (or possible
    smart pointers).

    struct DirEntry:Entry{

    vector<Entry*> ve;

    };

    You need to read up on polymorphism, which is what you are heading
    towards above.

    I'm not sure what you mean by a forwarding approach.

    john
    John Harrison, Sep 19, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page