简体   繁体   中英

Not sure how to call a class function using templates

I am making my own dictionary using templates (no I can't and I won't use anything from STL) .

I want to have a very simple search function, but I have a small problem.

template <typename TElement>
void Dictionary<TElement>::search(TElement ADT, int key) {  // Abstract Data Type
    inf flag = 0;
    index =  int (key % max);
    temp[index] = root[index]; // root of the hash
    while (temp[index]->next != NULL) {
        if(temp[index]->data->key_actual_name == key) { @things happen }
    }
}

What I want to understand: how to use template so that I can have temp[index]->data-><template call> if that makes any sense

I want to call the dictionary by using: Class_type == TElement and "key" is always an int but it can be different things. It might be an ID or phone number. Problem is I need to use the actual name of the key ( if(temp[index]->data->ID (or phone or what ever) == key ) { @things happen }), I imagine I could use templates here but I don't know how.

also maybe relevant:

template <typename TElement>
typedef struct list{
    TElement data;
    struct list *next;
}node_type;
node_type *ptr[max], *root[max], *temp[max]; 

Also, if I do use a template for key_actual_name, how would the implementation work AND how would I call that function ?

You can get some inspiration from standard library functions like find_if that has an extra parameter for the comparison.

template <class InputIterator, class Predicate>
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred );

You can then pass a parameter telling the search function how to find the key you are looking for. Perhaps replace the use of == with

if(pred(temp[index]->data, key)) { @things happen }

and pass different pred functions for comparing the key to the appropriate member.

If I understand correctly:

temp[index]->data->key_actual_name

resolves to a data member of TElement which is an int, and you want that to be a template. If that's the case you can do this:

template <template <class> class TElement, typename TKey>
struct node_type
{
    TElement<TKey> data;
    node_type *next;
};

template <template <class> class TElement, typename TKey>
class Dictionary
{
    typedef node_type<TElement, TKey> node_t;
    node_t _root;

    void search(node_t& node, const TKey& key)
    {
        TKey& thekey = node.data.key_actual_name;
        // Do some algorithm
    }
public:
    void search(const TKey& key)
    {
        search(_root, key);
    }
};

template <class T>
struct Element
{
    T key_actual_name;
};

int main(int argc, char ** argv)
{
    Dictionary<Element, int> dic1;
    dic1.search(1);

    Dictionary<Element, char> dic2;
    dic2.search('a');

    return 0;
}

As you can see, the Element has a template parameter, so you can change the type of key_actual_name to whatever suits your case, yet have the search function access it in a generic way (assuming it has the operator== overload).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM