I have custom binary tree class that holds values of template type T (it could be value or pointer). Each value is encapsulated with number (this number is used for searching in tree). I want to have an std::map inside my tree class fo fast O(1) access to objects without numbers.
template <typename T>
stuct BSTNode
{
T value;
int searchValue;
}
template <typename T>
class BST
{
BSTNode<T> * root;
std::map<T, BSTNode<T>> cache;
//... etc.
}
Example: I have class instance a inserted in tree under value n. Now I want to get the node associated with this a. I cannot search the tree, because I don’t have n. So I want to use a, and from std::map get node = map[a]. Now I can do node->n.
How can I achieve this? I can override compare method of std::map:
bool operator()(const void * s1, const void * s2) const
But it doesn’t work for value and pointer at the same time: cannot convert parameter 1 from const double to const void *.
Make a traited comparator:
Now your map can be defined like so: