In one of my projects I'm using a tree implementation, where I used the container
C=std::map<K,V>
to maintain a list of children for each tree node. Each tree node has a unique name key K
being normally a std::string
.
template<typename V, template<typename Key=std::string,typename Type=TreeNode<V>,typename ...> typename C>
class TreeNode {
typedef C<std::string, Value> cont_type;
typedef V data_type;
cont_type childs;
data_type value;
cont_type::iterator genericFind(const K& k) {
// Something generic here!!!
}
}
This implementation worked quite well for me besides the fact, that std::map doesn't respects the order of insertion in the tree. For some applications I need to keep the order of insertion, but for other applications the need for fast information retrivial is more important.
Hence C
needs to be either of type
std::vector<std::pair<K, V>> // keeping insertion order
or
std::map<K,V> // fast information retrivial
No I have a problem with the implementation of my TreeNode
class, still using explicitly the interface of std::map
. Especially, it uses the member functions find
, erase
, insert
that needed to be replaced by something generic, where the implementation for the both container types is quite concrete.
For example childs.find(key)
needs to be replaced by find(childs.begin(), childs.end(), key)
, whenever I plugin the std::vector
implementation.
Maybe there is another solution, that I'm not aware of. This might be maybe boost::multi_index
, but I'm quite unsure about this.
What might be the easiest way to solve my issue?