General STL data-structure design problem

Suppose I have a big pile of papers, and need to sort them into chapters, then sort the chapters into a book.

But then suppose the above-mentioned book is really just a page in a chapter of a larger book, and this might go arbitrarily deep, or wide. Regardless of the dimensions, the memory footprint will be tiny, so that's not a concern...I just need to cook up some kind data-structure, maybe a tree or something, to store stuff in an ordered fashion.

Having a bit of trouble wrapping my head around the whole thing, and I'd really like to avoid having to do any manual memory management. I have some foggy notions of linked-lists, maps, pointers, trees, red-black-things, but nothing is coming together in any sort of recognizable solution.

If anyone has some input on how I might do this, it's greatly appreciated.

the <map> containers seems really well
I have troubles understanding what exactly you are trying to do, could you please go a little bit more into detail?
I guess I'm looking for some variable-dimensioned container, like a map<key, element> container, where the element is another map<key, element>, which could go arbitrarily deep, like a recursive definition or something:

struct Struct1{
map<string, Struct1> elements;
map<string, string> attributes;
};

The above has me wincing at what are probably some ugly logical flaws; my first post really kind of says it most clearly. I'll try to elaborate more tomorrow I think.



closed account (3hM2Nwbp)
A little food code for thought. If you have a compatible compiler, there is also std::shared_ptr in the 'memory' header (#include <memory> ).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <boost/smart_ptr.hpp>
#include <list>
class Node
{
  public:

    Node(boost::shared_ptr<Node> parent, const std::list<boost::shared_ptr<Node>>& children)
      : parent(parent), children(children)
    {

    }

    Node(boost::shared_ptr<Node> parent)
      : parent(parent)
    {

    }

    boost::shared_ptr<Node> getParent(void) const
    {
      return parent;
    }

    void addChild(boost::shared_ptr<Node> child)
    {
      children.push_back(child);
    }

    const std::list<boost::shared_ptr<Node>>& getChildren(void) const
    {
      return children;
    }

    virtual ~Node(void)
    {
      
    }

  private:

    boost::shared_ptr<Node> parent;

    std::list<boost::shared_ptr<Node>> children;
};

int main()
{
  Node root(nullptr);
  boost::shared_ptr<Node> child1(new Node(root));
  boost::shared_ptr<Node> child2(new Node(child1));
  root.add(child1);
  root.add(boost::shared_ptr<Node>(new Node(root))); // "child3"
  /*
    root
    |_
    |   child1
    |   |_
    |      child2
    |_
        child3
  */
  return 0;
}
Last edited on
Topic archived. No new replies allowed.