如何返回迭代器--list:<T>:迭代器,作为函数返回值

How to return an Iterator--list<T>:: iterator, as function return value

本文关键字:迭代器 gt 返回值 函数 何返回 --list lt 返回      更新时间:2023-10-16

我正在实现一个抽象哈希表容器。我的find()函数定义正确,工作正常,如下所示:

template <class HashedObj>
HashedObj& HashTable<HashedObj>::find(const HashedObj &x){
    typename list<HashedObj>::iterator itr;
    itr = std::find(theList[hash(x)].begin(), theList[hash(x)].end(), x);
    if(itr == theList[hash(x)].end())
        return ITEM_NOT_FOUND;
    else
        return *itr;
} 

然而,我想定义另一个叫做findAddress()的函数,它返回itr(迭代器)而不是*itr。我的代码是:

typedef list<HashedObj>::iterator iterator;
template <class HashedObj>
iterator HashTable<HashedObj>::find(const HashedObj &x){
    return std::find(theList[hash(x)].begin(), theList[hash(x)].end(), x);
} 

上面会报错:

type std::list<HashedObj, std::allocator<_CharT> > is not derived from type
  HashedTable<HashedObj>.

基本上,我想返回一个以前由std定义的迭代器类型

我不是专家,但是两个函数在给定的代码中具有相同的名称,同时接受相同的参数。这正常吗?另外,我假设你的意思是"名为findAddress()的函数"而不是"名为findAddress()的类"。