1

I try to make a templated Trie structure. It represents tree in internal private struct Node, which contains TElem, bool which indicates that this particular node is a terminal and a vector of children:

template<typename TElem>
class Trie
{
    // ...
private:
    struct Node
    {
        TElem elem;
        bool isTerminal;
        std::vector<std::shared_ptr<Node>> children;
    };

    Node root_;
};

Now I'd like to make another template parameter that would make possible to select another underling container, e.g. list. How can it be done?

Archie
  • 6,391
  • 4
  • 36
  • 44

1 Answers1

2

With template template parameters (untested code, but the general idea should be OK):

template <typename TElem, 
          template <typename, typename> class Cont
          template <typename> class Allocator=std::allocator>
class Trie {
 private:
    struct Node
    {
        typedef std::shared_ptr<Node> NodePtr_;
        TElem elem;
        bool isTerminal;
        Cont<NodePtr_, Allocator<NodePtr_>> children;
    };

    Node root_;
};

then

Trie<int, std::list> myTrie = ....;
juanchopanza
  • 223,364
  • 34
  • 402
  • 480