我正在使用模板创建自己的字典(不,我不能,我不会使用STL中的任何内容).
我想要一个非常简单的搜索功能,但我有一个小问题.
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 }
    }
}
我想要了解的内容:如何使用模板,以便我可以拥有,temp[index]->data-><template call>如果这是有道理的
我想通过使用来调用字典:Class_type == TElement和"key"总是一个int但它可以是不同的东西.它可能是ID或电话号码.问题是我需要使用密钥的实际名称(if(temp[index]->data->ID (or phone or what ever) == key){@things happen}),我想我可以在这里使用模板,但我不知道如何.
也许相关:
template <typename TElement>
typedef struct list{
    TElement data;
    struct list *next;
}node_type;
node_type *ptr[max], *root[max], *temp[max]; 
另外,如果我使用key_actual_name的模板,实现将如何工作以及如何调用该函数?
您可以从标准库函数中获得一些灵感,例如find_if具有用于比较的额外参数.
template <class InputIterator, class Predicate>
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred );
然后,您可以传递一个参数,告诉search函数如何找到您要查找的密钥.也许替换使用的==用
if(pred(temp[index]->data, key)) { @things happen }
并传递不同的pred函数以将密钥与适当的成员进行比较.
| 归档时间: | 
 | 
| 查看次数: | 122 次 | 
| 最近记录: |